[英]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")
Of course, to get dimensions right, use eg pdf
or png
device, as it is well explained in this answer . 当然,为了获得正确的尺寸,请使用例如
pdf
或png
设备,因为在这个答案中有很好的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.