r - Arranging ggplot multiple objects maintaining while constant height -
i'm trying split map of united states multiple windows (some of contain same state twice). i'd scales constant (so maps aren't distorted) minimize space between maps. can't use facet_wrap (due overlapping nature of regions--and anyway, facet_wrap can't have scales both fixed , have different xlims each window). suggestions on how improve spacing on results?
require(data.table) require(ggplot2) require(maps) require(gridextra) all_states <- as.data.table(map_data("state")) setnames(all_states,"region","state") ##define regions overlapping states weco.states <- c("oregon","washington","california") west.states <- c("washington","montana", "idaho","utah","nevada","arizona","new mexico", "wyoming","colorado","south dakota","texas") east.states <- c(setdiff(unique(all_states$state), union(weco.states,west.states)), "texas","south dakota") all_states[,c("weco","west","east"):=false] all_states[state%in% weco.states, weco:=true] all_states[state%in% west.states, west:=true] all_states[state%in% east.states, east:=true] p.regbase <- ggplot() + coord_equal() +ylim(c(25,50)) p.weco <- p.regbase + geom_polygon(data=all_states[(weco),], aes(x=long, y=lat, group = group),colour="white", fill="grey" ) p.west <- p.regbase + geom_polygon(data=all_states[(west),], aes(x=long, y=lat, group = group),colour="white", fill="grey" ) p.east <- p.regbase + geom_polygon(data=all_states[(east),], aes(x=long, y=lat, group = group),colour="white", fill="grey" ) print(arrangegrob(p.weco,p.west,p.east,ncol=3,nrow=1))
depending on how resize graphics window in windows gui, results either bad (scales different)
or decent (same heights) there's space: how can rid of space?
here solution using facet_grid()
, obscure setting theme(aspect.ratio=1)
. plot not perfect, hope gives of need. note states bit wider should. apparently 1 degree of latitude noticeably not same distance 1 degree of longitude in usa.
# create new factor column faceting. newfactor = ifelse(all_states$weco, "weco", ifelse(all_states$west, "west", "east")) # manually specify sort order of factor levels. newfactor = factor(as.character(newfactor), levels=c("weco", "west", "east")) all_states$region = newfactor plot_1 = ggplot(all_states, aes(x=long, y=lat, group=group)) + geom_polygon(colour="white", fill="grey") + facet_grid(. ~ region, space="free", scales="free") + theme(aspect.ratio=1) ggsave("plot_1.png", plot=plot_1, height=4, width=8, dpi=150)
Comments
Post a Comment