Ordering multiple ggplot objects at a constant height

I am trying to split a map of merged states into several windows (some of which contain the same state twice). I would like the scales to be constant (so that the cards are not distorted), but also minimize the space between the cards. I cannot use facet_wrap (due to the overlapping nature of the regions), and in any case, facet_wrap cannot scale both fixed and different xlim for each window). Any suggestions on how to improve the interval of 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 with 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 I resize the graphical window in the Windows GUI, the results are either bad (the scales are different) enter image description here

or decent (the same height), but there is too much space: how can I get rid of excess space?

enter image description here

+6
source share
2 answers

Here is a solution using facet_grid() and a somewhat obscure theme(aspect.ratio=1) setting theme(aspect.ratio=1) . The plot is not perfect, but I hope that it will give you most of what you need. Please note that states look a bit wider than they should. Apparently, 1 degree of latitude does not noticeably coincide with 1 degree of longitude in the United States.

 # Create a new factor column for 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) 

enter image description here

+5
source

Let's clarify a few things.

  • grid.arrange doesnโ€™t โ€œadd registrationโ€, it just puts the grob side by side in the grid layout. You can change the width of each cell in a row,

 grid.newpage() pushViewport(viewport(width=5, height=2.5, default.units="in")); gt = grobTree(rectGrob(), circleGrob()) grid.arrange(gt, gt, gt, nrow=1, newpage=FALSE, widths = unit(c(1, 2, 3), "null")) upViewport() 

enter image description here

  • Aligning graphs is easy when they do not have a fixed aspect ratio, for example. using gtable: cbind_gtable

  • the fixed aspect ratio is encoded in gtable through the relative units of the zero grid, as you can easily check


 g = ggplotGrob(p.weco) g[["heights"]][3] # 2.37008405379269 is the aspect ratio for that plot panel 

  • the reason indicating that the width does not work well with the fixed ggplot2 ratio is twofold: i) it is difficult to guess which relative width we should assign, since it depends on the aspect ratio that was calculated according to the data ranges; ii) the width and height of the device must also be set in accordance with three proportions in order to avoid an extra space (necessary to maintain the correct proportions).

These issues have been discussed here ; I do not know an elegant and general solution.

+5
source

Source: https://habr.com/ru/post/950762/


All Articles