繁体   English   中英

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

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

我试图使用plot_grid()函数TraMineR使用TraMineR包创建的几个“状态序列对象”图。 我的主要问题是,我无法将使用seqplot()函数创建的图存储在列表中。 我的问题不在于如何定位图例或“子”图,而是关于如何在网格中实际绘制seqplots。

我将ggscatter图存储在一个列表中,然后将该列表传递给plot_grid()函数 - 所以我相对肯定这一点是有效的。 我认为问题主要是seqplot()产生的对象类型。 我尝试使用as.ggplot()函数存储图,但这不起作用(下面的示例A)。

使用recordPlot()函数或多或少有效。 虽然它看起来不太好(下面的例子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)
)

我希望相对自由地定位我想要在网格中绘制的实际元素。 例如,我想在一个5 x 3网格中保存一个包含13个这样的图和一个图例的页面 - 如示例中所示。 这就是为什么我认为与plot_grid()相比,例如par(mfrow = c(5,3))更好。 另外,使用par()我得到“plot.new()中的错误:数字边距太大”。

绘图网格示例

seqplot内部使用基础plot 因此,绘制多个图形可以更好地适应layout 您只需制作一个布局矩阵并绘制一个接一个的图。 传说seqlegend也可以视为情节。 这里有一个包含数据的示例和带有图例的四个图。

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

产量

在此输入图像描述

当然,为了获得正确的尺寸,请使用例如pdfpng设备,因为在这个答案中有很好的解释。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM