r - 如何将 ggplot 旋转为横向?

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

最佳答案

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

关于r - 如何将 ggplot 旋转为横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12441594/

相关文章:

r - ggplot2 R 上的连续到离散图例

r - 使用 ggplot2 中的标签更改 facet_wrap 标签

r - kernlab 中 rbfKernel 的方程与标准不同?

r - 在终端中更改 R 语言设置

r - "' knitr ' not found"打包/小插图构建期间的错误

r - 从多个 Rmd 文件缓存 knit 外部代码

R:按嵌套列表中的值对列表进行排序

通过簇替换重新采样

r - 控制knitr中两个并排图的对齐

r - 如何调用由 downloadHandler 中的 react 函数产生的图