9

I would like to rotate a complete ggplot object 90°.

I do not wish to use coord_flip as this appears to interfere with scale="free" and space="free" when using facets.

For example:

qplot(as.factor(mpg), wt, data=mtcars)+
facet_grid(.~vs + am, scale="free",space="free")

enter image description here

vs

qplot(as.factor(mpg), wt, data=mtcars)+
  facet_grid(vs + am ~ ., scale="free",space="free")+
  coord_flip()

enter image description here

What I would like:

enter image description here

I would likely need to use gridExtra.

Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • Why do you need to rotate with ggplot2/grid? It's probably easier to rotate in whichever software (e.g., latex or word) you want to use this. – Roland Jun 19 '14 at 17:27
  • If you want to rotate a plot within a Rmarkdown document you can simply use the chunk option out.extra='angle=-90' . – fry Dec 16 '21 at 16:41
  • related https://stackoverflow.com/questions/39712801/how-to-combine-two-ggplots-with-one-rotated – tjebo Aug 21 '22 at 17:41

2 Answers2

17
print(p, vp=viewport(angle=-90))
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • The laborious alternative I had found was `grid.newpage() pushViewport(viewport(angle=-90, width = unit(7, "inches"), height = unit(7, "inches"))) grid.draw(ggplot_gtable(ggplot_build(p)))` – Etienne Low-Décarie Jun 19 '14 at 17:42
  • 1
    Good one. You are the gridly-Ace. (I would probably have just printed as "landscape", but I can see the value of being able to send this in knitr or other markup systems. – IRTFM Jun 19 '14 at 17:53
  • 1
    @EtienneLow-Décarie if you have pushed a viewport, you can use `print(p, newpage=FALSE)`. And if for some reason you want to draw a grob explicitly, `ggplotGrob` is a shorter alias. – baptiste Jun 19 '14 at 18:07
2

Is this what you were expecting?

qplot(x=wt, y=as.factor(mpg), data=mtcars) +
  facet_grid(vs + am ~ ., scale="free", space="free")

"rotated" plot

ialm
  • 8,510
  • 4
  • 36
  • 48