- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 cowplot 包的 plot_grid() 函数在网格中绘制 ggplots,想知道是否有办法按列而不是按行绘制图?
library(ggplot2)
library(cowplot)
df <- data.frame(
x = c(3,1,5,5,1),
y = c(2,4,6,4,2)
)
# Create plots: say two each of path plot and polygon plot
p <- ggplot(df)
p1 <- p + geom_path(aes(x,y)) + ggtitle("Path 1")
p2 <- p + geom_polygon(aes(x,y)) + ggtitle("Polygon 1")
p3 <- p + geom_path(aes(y,x)) + ggtitle("Path 2")
p4 <- p + geom_polygon(aes(y,x)) + ggtitle("Polygon 2")
plots <- list(p1,p2,p3,p4)
plot_grid(plotlist=plots, ncol=2) # plots are drawn by row
我想在第一列中绘制 P1 和 P2,在第二列中绘制 p3 和 p4,例如:
plots <- list(p1, p3, p2, p4) # plot sequence changed
plot_grid(plotlist=plots, ncol=2)
实际上我可以有 4、6 或 8 个图。绘图网格中的行数会有所不同,但总是有 2 列。在每种情况下,我都希望按列(垂直)填充绘图网格,以便我的前 2、3 或 4 个绘图(视情况而定)相互重叠。如果我可以指定像 par(mfcol = c(n,2)) 这样的东西,我想避免对这些不同的排列进行硬编码。
最佳答案
正如您所观察到的,plot_grid()
按行绘制图。我不相信有任何方法可以改变它,所以如果你想保持使用 plot_grid()
(这可能是最方便的),那么一种方法可能是更改绘图列表中项目的顺序以匹配 plot_grid()
所需的内容。 ,给定列数的知识。
这是我编写的一个函数,可以做到这一点。基本思想是:
1:length(your_list)
),reorder_by_col <- function(myData, col_num) {
x <- 1:length(myData) # create index vector
length(x) <- prod(dim(matrix(x, ncol=col_num))) # adds NAs as necessary
temp_matrix <- matrix(x, ncol=col_num, byrow = FALSE)
new_x <- unlist(split(temp_matrix, rep(1:ncol(temp_matrix), each=row(temp_matrix))))
names(new_x) <- NULL # not sure if we need this, but it forces an unnamed vector
return(myData[new_x])
}
这一切都是在谷歌的帮助下写的,特别是对发布的问题的回答
here和
here .
plots <- list(p1,p2,p3,p4)
plot_grid(plotlist=plots, ncol=2)
newPlots <- reorder_by_col(myData=plots, col_num=2)
plot_grid(plotlist=newPlots, ncol=2)
关于ggplot2 - 按列在绘图网格中绘制ggplots——cowplot包的plot_grid()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63074633/
这是关于@ClausWilke ( see post ) 提供的解决方案之一的后续问题,用于在主面板和边际图之间插入间隙。如何决定 (scale_x_continuous) 限制?另外,如果我们使用“
在当前图中,p2 现在居中。我希望 p2 与左侧的 p1 对齐。我尝试了 plot_grid 中的一些参数,但图像根本没有改变。 library(ggplot2) library(flextable)
我正在尝试为使用 cowplot::plot_grid() 组合 ggplot2 绘图的函数编写单元测试。例如, # setup set.seed(123) library(ggplot2) # cr
假设我有一个数据集 x和 y根据两个变量分组的值:grp是 a , b , 或 c , 而 subgrp是 E , F , 或 G . a有 y [0, 1] 中的值 b有 y [10, 11] 中的
我正在尝试使用cowplot包的draw_image()函数。我已经设法在图表中获取图像作为示例。 我无法弄清楚 xy 位置是如何工作的,我必须不断输入随机数,直到看到图像。 require(ggpl
我正在尝试使用cowplot包的draw_image()函数。我已经设法在图表中获取图像作为示例。 我无法弄清楚 xy 位置是如何工作的,我必须不断输入随机数,直到看到图像。 require(ggpl
我正在尝试使用 Cowplot 制作一些“紧密堆叠”的图。我可以使用以下 MWE 获得我想要的效果: library(ggplot2) library(cowplot) disp_plot <- gg
我正在尝试制作一个相关热图,其中使用 cowplot::switch_axis_position 将 x 轴移动到顶部.我有不同长度的轴标签,我希望标签左对齐(或者更确切地说是底部对齐,因为它们旋转了
我想以某种方式表明应该将多面板图中的某些行放在一起进行比较。例如,我想制作这个情节: 看起来像这个图(用 PowerPoint 制作的面板周围有框): 这是我为使用第一个图而制作的代码。我使用了 gg
我有一个多图图,由 2x2 配置的 4 个图组成。我使用“cowplot”包和 plot_grid 函数使用下面的代码排列图 plot_grid(p1, p2, p3, p4, align='vh',
我正在尝试在与 plot_grid 对齐的两个图周围绘制边框来自牛图包。请看下面的例子(修改自 "Changing the axis positions" vignette ): require(gt
我有一个关于 Cowplot 中的绘图对齐的问题。 我想在由 3 列组成的面板中对齐图 (ggplot)。第一列有 2 个图(垂直对齐),第二列有 1 个图,第三列也有两个图(垂直对齐)。 示例: #
我已经使用 purrr 创建了 ggplot2 图形的列表列,现在我想使用 cowplot::plot_grid() 来组合他们成一个单一的情节。我怎样才能做到这一点?有一种蛮力方法可以做到这一点,但
我有一个关于 Cowplot 中的绘图对齐的问题。 我想在由 3 列组成的面板中对齐图 (ggplot)。第一列有 2 个图(垂直对齐),第二列有 1 个图,第三列也有两个图(垂直对齐)。 示例: #
我已经使用 purrr 创建了 ggplot2 图形的列表列,现在我想使用 cowplot::plot_grid() 来组合他们成一个单一的情节。我怎样才能做到这一点?有一种蛮力方法可以做到这一点,但
我有一个来自 1:10 的 ggplots 列表,名为 plot_1 , plot_2 ....plot_10 . 我想使用 cowplot 将所有图一起显示。 我如何使用 plot.grid()调用
我正在尝试使用 cowplot::plot_grid() 组合两个 ggplot 对象并垂直对齐它们。这通常使用 align = "v" 非常简单。 dat1 <- data.frame(x = re
我想用 R 绘制一个看起来像用 Mac 的 Numbers 制作的示例的图。我正在为情节和图例框之间的空间而苦苦挣扎。这是我想要实现的示例: 在一些用户的帮助下(请参阅帖子末尾以供引用)我已经非常接近
我被问到 this question on Twitter并认为把它放在这里可能会很好。 使用 plot_grid() 制作带标签的并排图时,对于单字母标签,事情按预期工作: library(cowp
使用cowplot中的draw_image()函数读取矢量化pdf有时会导致图像严重模糊: library(ggplot2) library(cowplot) library(magick) # ma
我是一名优秀的程序员,十分优秀!