简体   繁体   English

ggplot2中的geom_polygon-不同填充物的刻面?

[英]geom_polygon in ggplot2 - Facets for different fillings?

is there a way of combining these three maps using facets? 有没有办法使用构面组合这三个地图?

emi <- readShapePoly('prov2011_g.shp')
names(emi) <- c('REG', 'ID', 'NOME', 'SHAPE', 'AREA', 'MIG1', 'MIG2', 'MIG3')
emi_geom <- poly_coords(emi)

map <- qplot(PolyCoordsY, PolyCoordsX, data = emi_geom, group = Poly_Name, geom = 'polygon', fill = MIG1)

map1 <- qplot(PolyCoordsY, PolyCoordsX, data = emi_geom, group = Poly_Name, geom = 'polygon', fill = MIG2)

map2 <- qplot(PolyCoordsY, PolyCoordsX, data = emi_geom, group = Poly_Name, geom = 'polygon', fill = MIG3)

I followed this great tutorial ( http://www.r-bloggers.com/maps-with-ggplot2/ ). 我遵循了这个很棒的教程( http://www.r-bloggers.com/maps-with-ggplot2/ )。 The poly_coords function exctracts coordinates out of a shapefile. poly_coords函数从shapefile中提取坐标。 I have tried to use melt (reshape2) but I don't know how to apply it to this case - with other geom_* is a lot easier. 我曾尝试使用melt(reshape2),但我不知道如何将其应用于这种情况-使用其他geom_ *会容易得多。 Hope you can help me out. 希望你能帮助我。

Posting an answer so that no future SO searcher thinks that qplot() is even a remotely good idea. 发布答案,这样将来的SO搜索者都不会认为qplot()甚至是个好主意。 No accept or +1's required. 无需接受或+1。 Please just stop cutting & pasting code from blog posts without even trying to grok it. 请停止从博客文章中剪切和粘贴代码,甚至不尝试使用它。

library(rgdal)
library(maptools)
library(ggplot2)
library(viridis)

prov <- readOGR("prov2011_g.shp", "prov2011_g", stringsAsFactors=FALSE, verbose=FALSE)

longlat <- "+init=epsg:4121 +proj=longlat +ellps=GRS80 +datum=GGRS87 +no_defs +towgs84=-199.87,74.79,246.62"

prov <- SpatialPolygonsDataFrame(spTransform(prov, CRS(longlat)), prov@data)

prov_map <- fortify(prov, region="NOME_PRO")

fac <- data.frame(area=rep(prov@data$NOME_PRO, 3),
                  measure=rep(c("A", "B", "C"), each=nrow(prov@data)),
                  val=sample(100, nrow(prov@data)*3, replace=TRUE))

gg <- ggplot()
gg <- gg + geom_map(data=prov_map, map=prov_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.1, fill=NA)
gg <- gg + geom_map(data=fac, map=prov_map,
                    aes(fill=val, group=measure, map_id=area))
gg <- gg + scale_fill_viridis()
gg <- gg + facet_wrap(~measure)
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
gg

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM