1

我运行了一个包含 ggplot 函数的循环,但没有产生任何对象或绘图。问题是控制台或降价中也没有出现错误标志。图例(代码):

  • anova_total:包含 table_for_statistics 中所有蛋白质的 anova p 值的表
  • table_for_statistics:包含我需要的所有数据的透视表(但没有方差分析结果)。

从 anova_total 中,我过滤了所有 3 个变量所需的具有特定 p 值的所有蛋白质,并获得了这些蛋白质的 ID(它们是 anova_total 中的行名),因为我不需要 anova 结果本身我必须做的情节。我只需要知道我必须绘制的蛋白质的 ID;我需要绘制的特征包含在 table_for_statistics 中。

这是我的代码:

for (k in colnames(anova_total)){
imp.prot.cat <- table_for_statistics[table_for_statistics$protein %in% sig.total$sig.total,]

print(ggplot(table_for_statistics[table_for_statistics$protein %in% imp.prot.cat,], aes(x= table_for_statistics[table_for_statistics$protein %in% imp.prot.cat, k], y= measurement))+
  geom_beeswarm(aes(col= iss_group), shape=21, size=2,cex = 0.5)+
  facet_wrap(~protein, scale ="free_y")+
  theme(legend.position = "bottom") + 
  stat_compare_means(method = "t.test"))

 }

您可以通过单击以下链接查看循环中的数据集: https ://i.stack.imgur.com/kwFsS.png

谢谢!

4

1 回答 1

2

在此链接中,您可以很好地解释 ggplot 如何处理循环。

https://statisticsglobe.com/print-ggplot2-plot-within-for-loop-in-r

我们需要将打印函数包裹在创建绘图的 R 代码周围。使用带有秒数的下一个函数将显示具有指定时间延迟的图。

Sys.sleep(2)

在你的情况下:

for (k in seq_along(anova_total)){
imp.prot.cat <- table_for_statistics[table_for_statistics$protein %in% sig.total$sig.total,]

print(ggplot(table_for_statistics[table_for_statistics$protein %in% imp.prot.cat,], aes(x= table_for_statistics[table_for_statistics$protein %in% imp.prot.cat, k], y= measurement))+
  geom_beeswarm(aes(col= iss_group), shape=21, size=2,cex = 0.5)+
  facet_wrap(~protein, scale ="free_y")+
  theme(legend.position = "bottom") + 
  stat_compare_means(method = "t.test"))
Sys.sleep(2)
 }

通过使用 colnames 您正在检索或设置列的名称,尝试使用 seq_along() 从 k 移动到所有数据帧直到结束。即使您没有错误,您也可以通过调用 warnings() 来检查警告。

于 2021-09-12T16:26:36.410 回答