R 如何使用geom_多边形的镶嵌面生成地图网格
我试图使用刻面生成多个填充了不同值的贴图 我已经创建了下面的简化示例,它再现了我正在尝试做的事情和我不希望从ggplot中得到的结果。我使用美国地图,为各州生成两个假设社区。我可以分别绘制每个社区,但如果我尝试分面并同时生成它们,我只能得到一张地图R 如何使用geom_多边形的镶嵌面生成地图网格,r,ggplot2,facets,R,Ggplot2,Facets,我试图使用刻面生成多个填充了不同值的贴图 我已经创建了下面的简化示例,它再现了我正在尝试做的事情和我不希望从ggplot中得到的结果。我使用美国地图,为各州生成两个假设社区。我可以分别绘制每个社区,但如果我尝试分面并同时生成它们,我只能得到一张地图 require(ggplot2) require(maps) map <- map_data("state") states <- unique(map$region) # generate some hypothetical com
require(ggplot2)
require(maps)
map <- map_data("state")
states <- unique(map$region)
# generate some hypothetical communities
runA <- data.frame(region=states, id="A",
community=rbinom(length(states),1,.5))
runB <- data.frame(region=states, id="B",
community=rbinom(length(states),1,.5))
membership <- rbind(runA, runB)
# plot an individual map of communities from run A
df <- merge(map, runA, by="region")
ggplot(df) +
aes(long, lat, group=group) +
coord_equal() +
geom_polygon(aes(fill = as.factor(community)))
# likewise for B
df <- merge(map, runB, by="region")
ggplot(df) +
aes(long, lat, group=group) +
coord_equal() +
geom_polygon(aes(fill = as.factor(community)))
# now instead do one plot with two maps from facetting on id
df <- merge(map, membership, by="region")
ggplot(df) +
aes(long, lat, group=group, facets= id ~.) +
coord_equal() +
geom_polygon(aes(fill = as.factor(community)))
require(ggplot2)
需要(地图)
map您刚刚以错误的方式指定了面。这样做会很好:
ggplot(df) +
aes(long, lat, group=group) +
coord_equal() +
geom_polygon(aes(fill = as.factor(community))) +
facet_grid(facets= id ~.)
谢谢!为什么可以在aes()
中指定facets
美学?我认为它是这样工作的,因为faceting系统本身实际上不是美学,而是一个独立的绘图组件,如比例和坐标。因此,它可能是facet\u grid
,facet\u wrap
,或者将来可能是其他的,而这不能完全用标准的美学映射来指定。