简体   繁体   English

在 r 中包装地块多次给我相同的 plot

[英]wrap plots in r gives me the same plot multiple times

library(patchwork)
library(ggplot2)

cell_measurements = read_csv(random.csv)
measurement_types = col_names(cell_measurements)[5:9]
plot_list = list()

for (i in 1:5){
  plot_list[[i]] = ggplot(cell_measurements, aes(sapply(Injury, as.character), eval(parse(text = measurement_types[i])))) +
  geom_boxplot(aes(fill=Animal)) +
  labs(x ='Injury', y = measurement_types[i])
}

wrap_plots(plot_list)

Example data:示例数据:

Animal = c(dog, dog, dog, rabbit, rabbit, rabbit)
Injury = c(0,1,0,1,0,1)
Measurement1 = c(0.1,0.2,-0.8,1.5,1.2,1.3)
Measurement2 = c(0.1,0.5,-0.9, -1.4, 1.6, -0.5)

What I expected to get were five different plots neatly arranged, instead, all five plots here are the same.我期望得到的是整齐排列的五个不同的地块,相反,这里的所有五个地块都是相同的。 They have the right labels on the x and y axis but the box plots look exactly the same.它们在 x 轴和 y 轴上有正确的标签,但箱线图看起来完全一样。

Also, tried to each plot individually and then do wrap_plots which worked fine.此外,分别尝试每个 plot,然后执行wrap_plots ,效果很好。 Just doesn't work when I use the for loops.当我使用 for 循环时,它不起作用。

The lazy operation of ggplot makes it so that the value of i is really only evaluated when you try to plot all three. ggplot 的惰性操作使得i的值实际上仅在您尝试 plot 所有三个时才被评估。 That is, in your for loop, the i iterates over 1 through 5, but the ggplot code knows that it must reference i when it renders.也就是说,在您的for循环中, i遍历 1 到 5,但是 ggplot 代码知道它在呈现时必须引用i Since the rendering step doesn't happen until after the for loop is done, all five instances of the lazy-ggplot objects look for i and find the same value of i = 5 .由于渲染步骤直到for循环完成后才会发生,因此 lazy-ggplot 对象的所有五个实例都会查找i并找到i = 5的相同值。

Options:选项:

  1. Facet your plot, no need for wrap_plots .刻面你的 plot,不需要wrap_plots (This requires reshaping/pivoting/melting.) (这需要重塑/旋转/熔化。)

     reshape2::melt(quux, c("Animal", "Injury"), variable.name = "Measurement") |> ggplot(aes(Injury, Measurement)) + geom_boxplot(aes(fill = Animal)) + facet_grid(Measurement ~., scales = "free_y")

    带刻面的 ggplot

    You can use tidyr::pivot_longer in place of reshape2::melt .您可以使用tidyr::pivot_longer代替reshape2::melt

    You can facet them horizontally as well, though the y-axis will be split.您也可以将它们水平切面,但 y 轴将被拆分。

  2. Use lapply in place of your for loop.使用lapply代替您的for循环。 While it seems relatively synonymous in effect , there value of i is actually different between the iterations.虽然它看起来实际上是同义词,但i的值实际上在迭代之间是不同的。

     lapply(c("Measurement1", "Measurement2"), function(m) subset(quux, select = c("Animal", "Injury", m)) |> ggplot(aes(Injury, m)) + geom_boxplot(aes(fill = Animal))) |> ggplot(aes(Injury, m)) + geom_boxplot(aes(fill = Animal))) |> patchwork::wrap_plots()

    ggplot 与 wrap_plots


Data数据

quux <- structure(list(Animal = c("dog", "dog", "dog", "rabbit", "rabbit", "rabbit"), Injury = c(0, 1, 0, 1, 0, 1), Measurement1 = c(0.1, 0.2, -0.8, 1.5, 1.2, 1.3), Measurement2 = c(0.1, 0.5, -0.9, -1.4, 1.6, -0.5)), class = "data.frame", row.names = c(NA, -6L))

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

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