简体   繁体   中英

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/ ). The poly_coords function exctracts coordinates out of a 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. Hope you can help me out.

Posting an answer so that no future SO searcher thinks that qplot() is even a remotely good idea. No accept or +1's required. 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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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