Maiasaura
Maiasaura

Reputation: 32986

How do I rotate a ggplot to landscape?

I have a series of ggplots that I'd like to have arranged as shown below and inserted into a document parsed through knitr. Rather than have a really small portrait figure, I'd like to have this rotated to landscape so it can fill the page as much as possible. Any ideas?

library(ggplot2)
library(grid)
df <- data.frame(x = 1:100, y =rnorm(100))
plota <- ggplot(df, aes(x, y)) + geom_point(size = 4)


pushViewport(viewport(layout = grid.layout(3, 5)))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) 
        print(plota, vp = vplayout(1:2, 1:2)) 
        print(plota, vp = vplayout(1, 3)) 
        print(plota, vp = vplayout(1, 4))
        print(plota, vp = vplayout(1, 5))
        print(plota, vp = vplayout(2, 3)) 
        print(plota, vp = vplayout(2, 4))
        print(plota, vp = vplayout(2, 5))
        print(plota, vp = vplayout(3, 1))
        print(plota, vp = vplayout(3, 2))
        print(plota, vp = vplayout(3, 3))
        print(plota, vp = vplayout(3, 4))
        print(plota, vp = vplayout(3, 5))

Upvotes: 6

Views: 2245

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30124

It is easy to rotate a figure in LaTeX; you can use the option angle=90, as documented in http://yihui.name/knitr/options; see a full example below:

\documentclass{article}
\begin{document}

<<out.extra='angle=90'>>=
library(ggplot2)
library(grid)
df <- data.frame(x = 1:100, y =rnorm(100))
plota <- ggplot(df, aes(x, y)) + geom_point(size = 4)

pushViewport(viewport(layout = grid.layout(3, 5)))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) 
        print(plota, vp = vplayout(1:2, 1:2)) 
        print(plota, vp = vplayout(1, 3)) 
        print(plota, vp = vplayout(1, 4))
        print(plota, vp = vplayout(1, 5))
        print(plota, vp = vplayout(2, 3)) 
        print(plota, vp = vplayout(2, 4))
        print(plota, vp = vplayout(2, 5))
        print(plota, vp = vplayout(3, 1))
        print(plota, vp = vplayout(3, 2))
        print(plota, vp = vplayout(3, 3))
        print(plota, vp = vplayout(3, 4))
        print(plota, vp = vplayout(3, 5))
@

\end{document}

Upvotes: 6

Related Questions