4

I use ggplot2 and knitr to publish scatterplots with a right-hand-side legend. The legend is included in the aspect ratio and therefore breaks the "squareness" of the plots, as shown in the default themes. When the legend text becomes a bit longer than "a" and "b", the graphs become "long rectangles" instead of "squares".

I would like to keep the graphs "squared", and so would like to exclude the legend from the aspect ratio on my ggplot2 graphs. My .Rprofile has the following information to force ggplot2 to produce low-colour graphs with bigger text and more space around axis titles:

theme_set(theme_bw(16))
theme_update(
  axis.title.y = element_text(angle = 90, vjust = -.25),
  axis.title.x = element_text(vjust = -1),
  plot.margin = unit(c(1,1,1,1), "cm")
)

Is there anything I can add here to exclude the legend from the aspect ratio? Manipulations with coord_equal and coord_fixed have failed so far, as have the fig.width and fig.height options. Thanks for your help!

Edit: working example removed, question answered below with full example code (sorry for the delay in approving the answer).

Fr.
  • 2,865
  • 2
  • 24
  • 44

1 Answers1

5

Setting coord_fixed() should do the trick:

library(ggplot2)
library(gridExtra)  ## for grid.arrange()

## Create some data with one longer label
cars <- transform(mtcars, 
           cyl = factor(cyl, labels=c("4","6","8 is big")))


## Compute ratio needed to make the figure square
## (For a taller narrow plot, multiply ratio by number > 1; 
##  for a squatter plot, multiply it by number in the interval (0,1))    
ratio <- with(cars, diff(range(mpg))/diff(range(wt)))

## Make plots with and without a legend
a <- ggplot(cars, aes(mpg, wt)) + 
     geom_point() + coord_fixed(ratio=ratio)

b <- ggplot(cars, aes(mpg, wt, color=cyl)) + 
     geom_point() + coord_fixed(ratio=ratio)

## Plot them to confirm that coord_fixed does fix the aspect ratio
grid.arrange(a,b, ncol=2)

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • This does not produce any result on my end (see end of example): http://rpubs.com/briatte/3978 -- neither in `knitr` output or in RStudio plot windows. It could simply be that I don't understand well enough how the aspect ratio of the graphs are produced in that workflow. – Fr. Jan 31 '13 at 17:55
  • What about the example I added just now? If that doesn't work either, then there may be a problem with your setup. – Josh O'Brien Jan 31 '13 at 18:00
  • I must have been terribly unclear, because the issue I am trying to fix appears in your plots too. In your right-hand-side example, I am trying to keep the gray canvas square. I have outlined the surface here: http://i.imgur.com/s7SVuz6.png -- if the aspect ratio were 1:1, the surface would be square instead of rectangular. Instead, the whole plot is square. How do I get the legend out of the computation of the aspect ratio? – Fr. Jan 31 '13 at 23:21
  • @Fr. -- I finally get what you're after, and have edited my answer accordingly. – Josh O'Brien Jan 31 '13 at 23:37
  • Thank you very much, I think this is what I was looking for. I am still unsatisfied with how text ends up being so small in ggplot2, but that's a more general concern with ggplot2 output. Thanks again. – Fr. Feb 01 '13 at 00:57