我想制作一个趋势的空间地图,为此我使用我的变量 sen。我想用由点的大小确定的比例来表示这些值,如果值为负,则用蓝色表示,如果值为正,则用红色表示。这是我的代码:
library(ggplot2)
#Creating a funtion for the point size
point_size <- function(x) {
ifelse(x >= 0, 0.05 + x * 0.01, 0.05 - x * 0.01)
}
#Creating a funtion for color
color <- function(x) {
ifelse(x >= 0, "red", "blue")
}
#Data
data <- data.frame(Z = c(2.7, 1.8, 2.1, 1.4, -1.9, -2.3),
Sen = c(0.03, -0.03, 0.01, -0.015, 0.025, -0.05),
lon = c(-70.123, -71.456, -69.789, -72.012, -73.345, -68.901),
lat = c(-33.456, -32.012, -34.345, -31.678, -33.901, -35.234))
#Adjusting the size of the points
data$size <- point_size(data$Sen)
#Adjusting the color of the points
data$color <- color(data$Sen)
#Creating the dotmap with filled circles and black border
map_point <- ggplot(data, aes(x = lon, y = lat, size = size, fill = color)) +
geom_point(shape = 21, color = "black") +
scale_size_continuous(range = c(2, 8)) +
labs(title = "Map of points", x = "Longitude", y = "Latitude", size = "Size", fill = "Value") +
theme_minimal() +
scale_fill_manual(values = c("blue" = "blue", "red" = "red"),
labels = c("Negatives", "Positives"))
print(mapa_puntos)`
但我的目标是拥有一个从 -0.05 到 0.05 的刻度,步长为 0.01,因此图例中将有 10 个点:5 个蓝色和 5 个红色:这在 R 中可以做到吗?使用前面的代码,我得到的唯一结果就是单独表示信息。