向地图添加 continuous/gradient 填充
Adding continuous/gradient fill to map
潜伏了一段时间后,这是我的第一个问题——我是一个绝对的新手,所以请原谅我对这个论坛的习惯和 R 的技术细节缺乏了解。
我想绘制一张德国地图,其中联邦州的填充反映了居民的偏好,就像我在这里所做的那样:
我的数据如下:
Bundesland
Sausage
Baden-Württemberg
28,7%
Bayern
31,7%
Berlin
20%
Brandenburg
20,8%
Bremen
19,2%
Hamburg
20,6%
Hessen
24,4%
Mecklenburg-Vorpommern
24,8%
Niedersachsen
26,5%
Nordrhein-Westfalen
26,6%
Rheinland-Pfalz
28,9%
Saarland
27,3%
Sachsen
22,5%
Sachsen-Anhalt
25%
Schleswig-Holstein
26,6%
Thüringen
24,1%
我想这样绘制地图:
#loading libraries
library(ggplot2)
library(raster)
#retrieving map
germany <- getData(country = "Germany", level = 1)
#drawing map as plot
ggplot() +
geom_polygon(data = germany,
aes(x = long, y = lat, group = group))
它为我提供了这个 。
我现在的问题是:如何将上面 table 中的数据整合为不同联邦州的渐变填充?
并且:是否可以在各个联邦州的图中显示百分比,而不是在图旁边显示梯度图例?
非常感谢任何建议,非常感谢!
这应该提供一个启动器,使用简单的功能(包 sf
)并将香肠数据加入 Bundesland 数据。
我已经创建了香肠数据,假设百分比是数值,如果您的数据如 table 中所述(即“12,5%”字符串),则需要一些工作来转换这些数据(通常以可重现的方式向问题添加数据要好得多,即使用 dput(your_data)
的输出或作为数据框,即 dat <- data.frame(your_variables...)
)。
抱歉使用小数点表示法。
library(ggplot2)
library(raster)
library(dplyr)
library(sf)
dat <- structure(list(Bundesland = c("Baden-Württemberg", "Bayern",
"Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern",
"Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland",
"Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), Sausage = c(0.287, 0.317, 0.2, 0.208, 0.192, 0.206, 0.244,
0.248, 0.265, 0.266, 0.289, 0.273, 0.225, 0.25, 0.266, 0.241)), class = "data.frame", row.names = c(NA,
-16L))
germany <-
getData(country = "Germany", level = 1) %>%
st_as_sf() %>%
left_join(dat, by = c("NAME_1" = "Bundesland"))
ggplot(germany) +
geom_sf(aes(fill = Sausage))+
geom_sf_text(aes(label = scales::percent(Sausage)))+
scale_fill_gradient(low = "yellow", high = "red")+
theme(legend.position = "none")
由 reprex package (v2.0.1)
于 2022-02-20 创建
感谢彼得!这里有连续值:
library(tidyverse)
library(raster)
library(sf)
germany <-
getData(country = "Germany", level = 1) %>%
st_as_sf() %>%
left_join(df, by = c("NAME_1" = "Bundesland")) %>%
mutate(Sausage_dot = gsub('\,', '.', Sausage),
Sausage1 = parse_number(Sausage_dot))
ggplot(germany) +
geom_sf(aes(fill = Sausage1))+
geom_sf_text(aes(label = Sausage))+
theme_void() +
scale_fill_gradient2(low = "yellow", high = "red", na.value = NA)+
theme(legend.position = "top",
legend.spacing.y = grid::unit(-0.5, "cm"),
legend.spacing.x = unit(1.0, 'cm'),
plot.title = element_text(size=22,hjust = 0.5))
您可以像这样使用 terra
包来做到这一点
library(geodata)
library(terra)
dat <- data.frame(Bundesland = c("Baden-Württemberg", "Bayern", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern", "Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland", "Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), Sausage = c(0.287, 0.317, 0.2, 0.208, 0.192, 0.206, 0.244, 0.248, 0.265, 0.266, 0.289, 0.273, 0.225, 0.25, 0.266, 0.241))
germany <- geodata::gadm("Germany", 1, path=".")
germany = merge(germany, dat, by.x="NAME_1", by.y="Bundesland")
plot(germany, "Sausage", col=heat.colors(25))
text(germany, paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.8)
为了避免柏林和勃兰登堡标签的重叠(这里也隐藏了图例)
xy <- crds(centroids(germany))
i <- which(germany$NAME_1 == "Brandenburg")
xy[i, ] <- xy[i,] + c(0.2, -0.4)
plot(germany, "Sausage", col=heat.colors(25), lwd=2, legend=FALSE)
text(vect(xy), paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.7)
并重现您的 gray-scale 没有坐标轴或标题的地图:
plot(germany, "Sausage", col=rev(gray(1:20 / 20)), border=gray(0.9), axes=FALSE, legend=FALSE, main="", mar=0)
text(vect(xy), paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.7)
潜伏了一段时间后,这是我的第一个问题——我是一个绝对的新手,所以请原谅我对这个论坛的习惯和 R 的技术细节缺乏了解。
我想绘制一张德国地图,其中联邦州的填充反映了居民的偏好,就像我在这里所做的那样:
我的数据如下:
Bundesland | Sausage |
---|---|
Baden-Württemberg | 28,7% |
Bayern | 31,7% |
Berlin | 20% |
Brandenburg | 20,8% |
Bremen | 19,2% |
Hamburg | 20,6% |
Hessen | 24,4% |
Mecklenburg-Vorpommern | 24,8% |
Niedersachsen | 26,5% |
Nordrhein-Westfalen | 26,6% |
Rheinland-Pfalz | 28,9% |
Saarland | 27,3% |
Sachsen | 22,5% |
Sachsen-Anhalt | 25% |
Schleswig-Holstein | 26,6% |
Thüringen | 24,1% |
我想这样绘制地图:
#loading libraries
library(ggplot2)
library(raster)
#retrieving map
germany <- getData(country = "Germany", level = 1)
#drawing map as plot
ggplot() +
geom_polygon(data = germany,
aes(x = long, y = lat, group = group))
它为我提供了这个
我现在的问题是:如何将上面 table 中的数据整合为不同联邦州的渐变填充?
并且:是否可以在各个联邦州的图中显示百分比,而不是在图旁边显示梯度图例?
非常感谢任何建议,非常感谢!
这应该提供一个启动器,使用简单的功能(包 sf
)并将香肠数据加入 Bundesland 数据。
我已经创建了香肠数据,假设百分比是数值,如果您的数据如 table 中所述(即“12,5%”字符串),则需要一些工作来转换这些数据(通常以可重现的方式向问题添加数据要好得多,即使用 dput(your_data)
的输出或作为数据框,即 dat <- data.frame(your_variables...)
)。
抱歉使用小数点表示法。
library(ggplot2)
library(raster)
library(dplyr)
library(sf)
dat <- structure(list(Bundesland = c("Baden-Württemberg", "Bayern",
"Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern",
"Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland",
"Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), Sausage = c(0.287, 0.317, 0.2, 0.208, 0.192, 0.206, 0.244,
0.248, 0.265, 0.266, 0.289, 0.273, 0.225, 0.25, 0.266, 0.241)), class = "data.frame", row.names = c(NA,
-16L))
germany <-
getData(country = "Germany", level = 1) %>%
st_as_sf() %>%
left_join(dat, by = c("NAME_1" = "Bundesland"))
ggplot(germany) +
geom_sf(aes(fill = Sausage))+
geom_sf_text(aes(label = scales::percent(Sausage)))+
scale_fill_gradient(low = "yellow", high = "red")+
theme(legend.position = "none")
由 reprex package (v2.0.1)
于 2022-02-20 创建感谢彼得!这里有连续值:
library(tidyverse)
library(raster)
library(sf)
germany <-
getData(country = "Germany", level = 1) %>%
st_as_sf() %>%
left_join(df, by = c("NAME_1" = "Bundesland")) %>%
mutate(Sausage_dot = gsub('\,', '.', Sausage),
Sausage1 = parse_number(Sausage_dot))
ggplot(germany) +
geom_sf(aes(fill = Sausage1))+
geom_sf_text(aes(label = Sausage))+
theme_void() +
scale_fill_gradient2(low = "yellow", high = "red", na.value = NA)+
theme(legend.position = "top",
legend.spacing.y = grid::unit(-0.5, "cm"),
legend.spacing.x = unit(1.0, 'cm'),
plot.title = element_text(size=22,hjust = 0.5))
您可以像这样使用 terra
包来做到这一点
library(geodata)
library(terra)
dat <- data.frame(Bundesland = c("Baden-Württemberg", "Bayern", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern", "Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland", "Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), Sausage = c(0.287, 0.317, 0.2, 0.208, 0.192, 0.206, 0.244, 0.248, 0.265, 0.266, 0.289, 0.273, 0.225, 0.25, 0.266, 0.241))
germany <- geodata::gadm("Germany", 1, path=".")
germany = merge(germany, dat, by.x="NAME_1", by.y="Bundesland")
plot(germany, "Sausage", col=heat.colors(25))
text(germany, paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.8)
为了避免柏林和勃兰登堡标签的重叠(这里也隐藏了图例)
xy <- crds(centroids(germany))
i <- which(germany$NAME_1 == "Brandenburg")
xy[i, ] <- xy[i,] + c(0.2, -0.4)
plot(germany, "Sausage", col=heat.colors(25), lwd=2, legend=FALSE)
text(vect(xy), paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.7)
并重现您的 gray-scale 没有坐标轴或标题的地图:
plot(germany, "Sausage", col=rev(gray(1:20 / 20)), border=gray(0.9), axes=FALSE, legend=FALSE, main="", mar=0)
text(vect(xy), paste0(round(germany$Sausage * 100), "%"), halo=TRUE, cex=0.7)