gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:45:44 24 4
gpt4 key购买 nike

我有一系列 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com