如何将ggplot旋转到横向?

时间:2012-09-15 21:03:55

标签: r knitr ggplot2

我有一系列ggplot我想安排如下所示,并插入到通过knitr解析的文档中。我不想拥有一个非常小的人像,而是希望将其旋转到横向,以便尽可能地填充页面。有什么想法吗?

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

1 个答案:

答案 0 :(得分:6)

在LaTeX中旋转图形很容易;您可以使用http://yihui.name/knitr/options中记录的选项angle=90;请参阅下面的完整示例:

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