2012-09-15 84 views
6

我有一系列ggplot s,我想按照下圖所示進行排列,並插入到通過knitr解析的文檔中。我不希望有一個非常小的肖像人物,而是希望將它旋轉爲橫向,以便儘可能地填充頁面。有任何想法嗎?如何將ggplot旋轉到橫向?

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

回答

6

在LaTeX中很容易旋轉圖形;您可以使用選項angle=90,如http://yihui.name/knitr/options中所述;看到一個完整的例子如下:

\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} 
+1

謝謝儀輝,很多apprecciated。 – Maiasaura

相關問題