R 中的趋势空间图

问题描述 投票:0回答:1

我想制作一个趋势的空间地图,为此我使用我的变量 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 中可以做到吗?使用前面的代码,我得到的唯一结果就是单独表示信息。

r maps legend spatial trend
1个回答
0
投票

您可以通过在

breaks
函数内设置
labels
limits
scale_size_continuous
参数来获得所需的输出。使用您的数据并将限制设置为 -0.5 和 0.5,会生成空白图表,因为所有值都高于 0.5;因此,我将限制更改为 -0.6 和 0.6。考虑根据您的实际数据调整这些值。

scale_size_continuous(breaks = seq(-0.06, 0.06, 0.01),
                        labels = seq(-0.06, 0.06, 0.01),
                        limits = c(-0.06, 0.06))

© www.soinside.com 2019 - 2024. All rights reserved.