How to control aspect ratios and scales of facette

2019-08-09 22:20发布

问题:

I want to facet three plots in rows of a single column using ggplot2, as illustrated below.

library(ggplot2)    
df <- data.frame(x=rep(1,3), y=rep(1,3), z=factor(letters[1:3]))
p <- ggplot(df, aes(x, y)) + geom_point() + facet_grid(z ~ .)
p

There are two problems with this output. Most importantly, I want to control the scales of the x and y axes, in this case to make them the same i.e. a single unit should measure the same distance on both x and y axes.

The second issue is the colliding lables for y axis of the facetted plots. Bonus points for solving that, but full credit for the scale/aspect ratio problem.

回答1:

I think you are looking for coord_fixed

library(ggplot2)    
df <- data.frame(x=rep(1,3), y=rep(1,3), z=factor(letters[1:3]))
p <- ggplot(df, aes(x, y)) + geom_point() + facet_grid(z ~ .)
p + coord_fixed(ratio=1)



标签: r ggplot2