I am using ggplot to draw faceted maps and have been unable to work out how to allow "free" scales in each facet (so that small regions don't look too small) while keeping the x-y aspect ratio fixed.
Here is a simplified example:
require(maps) require(ggplot2) map_nz <- subset(fortify(map_data('nz')), region %in% c("South.Island ", "North.Island ")) gg_nz <- qplot(long, lat, data=map_nz, geom="polygon", group=group)
I now have a plot of the North and South Islands of New Zealand. I can facet this and display it with a fixed aspect ratio like this:
gg_nz + coord_fixed() + facet_wrap(~region)
with a result that looks like this:
Notice that there is quite a bit of space wasted in the North Island facet. I would like it to take up more of the available space. I can free up the scales like this:
gg_nz + facet_wrap(~region, scales="free")
with the following result:
The problem is that the x-y aspect ratio is no longer 1:1 in each facet. I am happy to have each facet on a different scale, but within the facet I would like to preserve the aspect ratio.
I tried the following without success:
gg_nz + facet_wrap(~region, scales="free") + coord_fixed()
Presumably the scale
parameter in facet_wrap
overrides coord_fixed
. Any suggestions?
UPDATE: to give a more dramatic illustration, here is the same phenomenon with some US states:
Fixed coords (using coord_fixed
or coord_equal
):
Free coords (using scales = free
):
Neither of these maps is ideal: in the first, Delaware is tiny. In the second, the aspect ratios are quite distorted. New Jersey, which is a narrow state, is stretched too wide, for example.
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.
facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .
theme(aspect.ratio = 1)
works.
gg_nz + facet_wrap(~region, scales="free") + theme(aspect.ratio = 1)
I believe what you are looking for is the ability to crop or zoom into a single facet. In other words, to adjust the ylim
s and xlim
s independently for each facet. It looks like such a feature has been requested but not yet implemented (https://github.com/hadley/ggplot2/issues/187)
If the space=free
option below does not work, an alternative is to scrap facets and use grid layouts and/or viewports to print each plot manually.
Using facet_grid
instead of facet_wrap
and adding space=free
:
gg_state + facet_grid(~region, scales = "free_x", space="free")
maybe instead of having both axis scale freely, just have only one scale:
map_state <- subset(fortify(map_data('state')), region %in% c("california", "nevada")) gg_state <- qplot(long, lat, data=map_state, geom="polygon", group=group) gg_state + facet_wrap(~region, scales="free_x")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With