Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facet with free scales but keep aspect ratio fixed

Tags:

r

ggplot2

gis

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:

NZ map - fixed aspect ratio

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:

NZ map - free scales

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

US map - Fixed aspect Free coords (using scales = free):

US map -Free scales

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.

like image 849
seancarmody Avatar asked Apr 27 '13 23:04

seancarmody


People also ask

What is the difference between facet wrap and facet grid?

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.

What does facet_ wrap do?

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.

What is facet_ grid?

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() .


2 Answers

theme(aspect.ratio = 1) works.

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

like image 169
JG11235 Avatar answered Sep 21 '22 07:09

JG11235


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

enter image description here


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

enter image description here

like image 31
Ricardo Saporta Avatar answered Sep 21 '22 07:09

Ricardo Saporta