我试图弄清楚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")
产生这个正确的图:
但是将
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")
最佳答案
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
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
奇怪的县映射的原因是县名不是唯一的。
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
请注意,此处的
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
请注意,在这里我们可以使用不同的空间对象,并在 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
最后一个:一个由三个彼此叠置的空间对象组成的复合体。请注意,如果您确实想映射县,因为它具有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
关于r - geom_map "map_id"函数如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912418/