r - geom_map "map_id"函数如何工作?

标签 r ggplot2

我试图弄清楚ggplot2中geom_map的使用。

设置:

library(ggplot2)
library(maps)
county2 <- map_data("county")

为什么这样的代码:
ggplot() +
  geom_map(data=county2, map=county2, aes(x=long, y=lat, map_id=region), col="white", fill="grey")

产生这个正确的图:
enter image description here

但是将map_id=region更改为map_id=subregion可以做到这一点吗?
ggplot() +
  geom_map(data=county2, map=county2, aes(x=long, y=lat, map_id=subregion), col="white", fill="grey")

enter image description here

最佳答案

geom_map()为您记住数据框中的多边形。

亚历克斯是正确的,map必须看起来像一个坚固的空间物体。那就是“记住”。 map_id可以是包含其他层的标识符的任何列。

您对geom_map()的第一个调用(通常)应该是“基础层”,类似于使用完整的GIS程序所执行的操作,该程序具有多边形轮廓并可能具有基础填充。

geom_map()的其他调用可以增加其他美感(包括其他shapefile)。

这里有一些例子来说明。

library(ggplot2)
library(maptools)
library(mapdata)
library(ggthemes)
library(tibble)
library(viridis)

us <- map_data("state")

choro_dat <- data_frame(some_other_name=unique(us$region),
                        some_critical_value=sample(10000, length(some_other_name)))

gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
                    aes(long, lat, map_id=region),
                    color="#2b2b2b", fill=NA, size=0.15)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(plot.margin=margin(20,20,20,20))
gg

enter image description here
county <- map_data("county")

gg <- ggplot()
gg <- gg + geom_map(data=county, map=county,
                    aes(long, lat, map_id=region),
                    color="#2b2b2b", fill=NA, size=0.15)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(plot.margin=margin(20,20,20,20))
gg

enter image description here

奇怪的县映射的原因是县名不是唯一的。
gg <- ggplot()
gg <- gg + geom_map(data=county, map=county,
                    aes(long, lat, map_id=subregion),
                    color="#2b2b2b", fill=NA, size=0.15)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(plot.margin=margin(20,20,20,20))
gg

enter image description here

请注意,此处的map_id既不是region也不是id,但仍然有效。 b/c该列中的值在us$region中。
gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
                    aes(long, lat, map_id=region),
                    color="#2b2b2b", fill=NA, size=0.15)
gg <- gg + geom_map(data=choro_dat, map=us,
                    aes(fill=some_critical_value,
                        map_id=some_other_name),
                    color="white", size=0.15)
gg <- gg + scale_fill_viridis(name="Value")
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(plot.margin=margin(20,20,20,20))
gg <- gg + theme(legend.position=c(0.85, 0.2))
gg

enter image description here

请注意,在这里我们可以使用不同的空间对象,并在 map 上包裹轮廓:
outline <- map_data("usa")

gg <- gg + geom_map(data=outline, map=outline,
                    aes(long, lat, map_id=region),
                    color="black", fill=NA, size=1)
gg

enter image description here

最后一个:一个由三个彼此叠置的空间对象组成的复合体。请注意,如果您确实想映射县,因为它具有FIPS代码(即每个可以映射美学的县的唯一ID),所以您想使用像this这样的名称。
state <- map_data("state")
county <- map_data("county")
usa <- map_data("usa")

gg <- ggplot()
gg <- gg + geom_map(data=county, map=county,
                    aes(long, lat, map_id=region),
                    color="#2b2b2b", fill=NA, size=0.15)
gg <- gg + geom_map(data=state, map=state,
                    aes(long, lat, map_id=region),
                    color="#2166ac", fill=NA, size=0.5)
gg <- gg + geom_map(data=usa, map=usa,
                    aes(long, lat, map_id=region),
                    color="#4d9221", fill=NA, size=1)
gg <- gg + coord_map("polyconic")
gg <- gg + theme_map()
gg <- gg + theme(plot.margin=margin(20,20,20,20))
gg

enter image description here

关于r - geom_map "map_id"函数如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912418/

相关文章:

r - 更改随机效应分组变量的标签会更改 lme4 中的结果

r - 在模拟中以长格式或数组格式存储数据

r - ggplot2 重命名 x 轴刻度更改图表外观

r - 如何处理ggplot2的 "data of class uneval"错误?

r - 从物种出生-死亡表计算总系统发育分支长度的总和

r - 与 rJava 和 xlsx 一起使用并行

r - ggplot2:将不连续的持续时间绘制为条形图

r - 在 ggplot2 中使用 ggrepel

r - ggplot : how to color points different than the connecting line

r - 循环数字序列