Facet with free scales but keep aspect ratio fixed

2020-05-30 06:59发布

问题:

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.

回答1:

UPDATE:

I believe what you are looking for is the ability to crop or zoom into a single facet. In other words, to adjust the ylims and xlims 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")


Original Answer:

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")



回答2:

theme(aspect.ratio = 1) works.

gg_nz + facet_wrap(~region, scales="free") + theme(aspect.ratio = 1)



回答3:

Does coord_equal help?

 gg_nz + facet_wrap(~region, scales="free") + coord_equal()

Edit

You could use grid.arrange to manually put the plots on a page, fixing the width and height. This seems to do the trick, but I can't make it work with facet() functionality. Thanks to this post from @mnel.

nz_sth <- qplot(long, lat, data=map_nz[map_nz$region == "South.Island ",], geom="polygon", group=group) + coord_fixed()
nz_nth <- qplot(long, lat, data= map_nz[ map_nz$region == "North.Island " , ] , geom="polygon", group=group) + coord_fixed()


grid.arrange( heights=unit(0.75, "npc") , widths=unit(0.5, "npc") , nz_sth,nz_nth, ncol=2)



标签: r ggplot2 gis