简体   繁体   English

如何使用R中的plot_grid()函数绘制几个seqplots(TraMineR包)?

[英]How to plot several seqplots (TraMineR package) using plot_grid() function in R?

I am trying to plot several plots of "state sequence object" created with the TraMineR package using the plot_grid() function. 我试图使用plot_grid()函数TraMineR使用TraMineR包创建的几个“状态序列对象”图。 My main problem is, that I can not store the plots created with the seqplot() function in a list. 我的主要问题是,我无法将使用seqplot()函数创建的图存储在列表中。 My problem is not about how to position the legend or "sub"plots but about how to actually plot the seqplots in a grid. 我的问题不在于如何定位图例或“子”图,而是关于如何在网格中实际绘制seqplots。

I stored ggscatter plots in a list and passed that list to the plot_grid() function before - so I am relatively sure that this works in general. 我将ggscatter图存储在一个列表中,然后将该列表传递给plot_grid()函数 - 所以我相对肯定这一点是有效的。 I think the problem is mainly the object type produced by seqplot() . 我认为问题主要是seqplot()产生的对象类型。 I tried to store the plots using the as.ggplot() function, which did not work (example A below). 我尝试使用as.ggplot()函数存储图,但这不起作用(下面的示例A)。

Using the recordPlot() function works more or less. 使用recordPlot()函数或多或少有效。 It does not look nice though (example B + C below). 虽然它看起来不太好(下面的例子B + C)。

library(TraMineR)
library(ggplotify)

data(biofam)
biofam.lab <- c("Parent", "Left", "Married", "Left+Marr",
                "Child", "Left+Child", "Left+Marr+Child", "Divorced")
biofam.seq <- seqdef(biofam[1:600,], 10:25, labels=biofam.lab)

# Example A ---------------------------------------------------------------

# not even able to store the plot
plot.list <- list()
plot.list[[1]] <- as.ggplot(seqplot(biofam.seq, type = "I", with.legend = FALSE, sortv = "from.start"))

# Example B and C ---------------------------------------------------------

plot.list <- list()
seqplot(biofam.seq, type = "I", with.legend = FALSE, sortv = "from.start")
plot.list[[1]] <- recordPlot()
seqplot(biofam.seq, type = "I", with.legend = FALSE, sortv = "from.start")
plot.list[[2]] <- recordPlot()
seqplot(biofam.seq, type = "I", with.legend = FALSE, sortv = "from.start")
plot.list[[3]] <- recordPlot()
seqplot(biofam.seq, type = "I", with.legend = FALSE, sortv = "from.start")
plot.list[[4]] <- recordPlot()

# Example B

plot_grid(plotlist = plot.list, ncol = 2)

# Example C

plot_grid(
  plot_grid(plotlist = plot.list, ncol = 2),
  plot_grid(plotlist = plot.list, ncol = 2)
)

I want to be relatively free in positioning the actual elements I want to plot in a grid. 我希望相对自由地定位我想要在网格中绘制的实际元素。 For example, I want to save a page containing 13 such plots and a legend in a 5 x 3 grid - as can be seen in the example. 例如,我想在一个5 x 3网格中保存一个包含13个这样的图和一个图例的页面 - 如示例中所示。 That's why I think going with plot_grid() works better compared to, eg, par(mfrow = c(5,3)) . 这就是为什么我认为与plot_grid()相比,例如par(mfrow = c(5,3))更好。 Additionally, using par() I get "Error in plot.new() : figure margins too large". 另外,使用par()我得到“plot.new()中的错误:数字边距太大”。

绘图网格示例

seqplot internally uses base plot . seqplot内部使用基础plot Therefore plotting multiple graphs would work better with layout . 因此,绘制多个图形可以更好地适应layout You just make a layout matrix and plot one plot after another. 您只需制作一个布局矩阵并绘制一个接一个的图。 The legend seqlegend can also be treated as a plot. 传说seqlegend也可以视为情节。 Here an example with your data and four plots with a legend. 这里有一个包含数据的示例和带有图例的四个图。

layout(matrix(c(1, 2, 2, 3:5), ncol=3, byrow=TRUE))
layout.show(5)  # this line is just to get the preview below where the plots will be placed

在此输入图像描述

seqplot(biofam.seq, type="I", with.legend=FALSE, sortv="from.start")
seqlegend(biofam.seq, cex=.9, ncol=5, position="bottom")  # legend
seqplot(biofam.seq, type="I", with.legend=FALSE, sortv="from.start")
seqplot(biofam.seq, type="I", with.legend=FALSE, sortv="from.start")
seqplot(biofam.seq, type="I", with.legend=FALSE, sortv="from.start")

Yields 产量

在此输入图像描述

Of course, to get dimensions right, use eg pdf or png device, as it is well explained in this answer . 当然,为了获得正确的尺寸,请使用例如pdfpng设备,因为在这个答案中有很好的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ggplot:使用RColorBrewer软件包填充Boxplots +使用R Cowxlot软件包中的plot_grid()绘制Boxplots - ggplot: Fill Boxplots Using the Package RColorBrewer + Plot Boxplots Using plot_grid() in the Cowplot Package in R Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? 将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图 - Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R 在 R 中使用 plot_grid 和 get_legend 的困难 - difficulty in using plot_grid and get_legend in R 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language 如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例? - How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package? 如何使用 R 中的 plot_grid 函数在图之间将 y 轴对齐 0? - How to align y axis by 0 between plots with plot_grid function in R? 如何修复错误:找不到 function“plot_grid”? - How to fix the error: could not find function "plot_grid"? 如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在 X 和 Y 轴上创建一个公共标题? - How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM