在 r 中循环创建多个箱线图。为什么只将“i”的最后一个值应用于重要性字母的计算和位置?

问题描述 投票:0回答:1

这是我的第一篇文章,我希望在正确的地方提出我的问题。 总之,我创建了一个循环来创建几个物种丰富度变量 (rich_X) 的箱线图,并通过显着性字母关联方差分析结果。我的问题如下:一旦我为循环定义了向量(loop.vector),箱线图就正确了,但是相关的 p 值及其在图表上的位置仅基于循环中的最后一个变量(这里是 rich_10m)。所以我最终得到了三个不同的箱线图(这很好),但具有相同的字母和相同的位置,基于 rich_10m。

这是我的代码:

dataPDG<-read.csv2("donnees moyennes.csv")#my data
max_rich<-read.csv2("max_rich.csv")#will be used to calculate letters position on plots

loop.vector <- c("rich_tot","rich_2m","rich_10m")#the 3 variables 
plot_list<-list()

for (i in loop.vector) {
  x <- as.data.frame(dataPDG[, c("Stations", i)])#Stations contains 5 replicats 
  x[, 2] <- as.numeric(x[, 2])

  p <- ggboxplot(x, x = "Stations", y = i, color = "Stations", add = "jitter", legend = "none") +
    rotate_x_text(angle = 45) +
    stat_compare_means(method = "anova", label.y = 13) +
    stat_compare_means(label = "p.signif", method = "anova", label.y = 12)
  
  max_values <- as.data.frame(max_rich[, c("Stations", i)])
  
  hsd <- HSD.test(aov(x[, 2] ~ Stations, data = x), trt = "Stations", group = T)
  sig.letter <- hsd$groups[order(row.names(hsd$groups)), ]
  sig.letter <- sig.letter[c("JD", "ES", "SG", "CC", "AO", "BA", "BO", "BR", "FE", "SN", "GF", "GP"), ]
  w <- max_values[[2]]
  
  d <- p + geom_text(data = max_values, aes(x = Stations, y = 0.5 + w, label = sig.letter$groups), vjust = 0)
  
  # Store the plot in the list with a name based on 'i'
  plot_list[[i]] <- d
}
plot_list[["rich_tot"]]
plot_list[["rich_2m"]]
plot_list[["rich_10m"]]

当我手动强制循环单个变量时(以检查代码是否正确运行),没有问题。它必须来自未正确更新为 i 值的 sig.letter 和 w。 这是无循环的工作代码:

dataPDG<-read.csv2("donnees moyennes.csv")#my data
max_rich<-read.csv2("max_rich.csv")#will be used to calculate letters position on plots

i<-"rich_tot"

  x <- as.data.frame(dataPDG[, c("Stations", i)])#Stations contains 5 replicats 
  x[, 2] <- as.numeric(x[, 2])

  p <- ggboxplot(x, x = "Stations", y = i, color = "Stations", add = "jitter", legend = "none") +
    rotate_x_text(angle = 45) +
    stat_compare_means(method = "anova", label.y = 13) +
    stat_compare_means(label = "p.signif", method = "anova", label.y = 12)
  
  max_values <- as.data.frame(max_rich[, c("Stations", i)])
  
  hsd <- HSD.test(aov(x[, 2] ~ Stations, data = x), trt = "Stations", group = T)
  sig.letter <- hsd$groups[order(row.names(hsd$groups)), ]
  sig.letter <- sig.letter[c("JD", "ES", "SG", "CC", "AO", "BA", "BO", "BR", "FE", "SN", "GF", "GP"), ]
  w <- max_values[[2]]
  
  d <- p + geom_text(data = max_values, aes(x = Stations, y = 0.5 + w, label = sig.letter$groups), vjust = 0)

你知道这可能来自哪里吗?非常感谢!

非常感谢!

r loops boxplot anova p-value
1个回答
0
投票

感谢您的这些评论。 为了使用 lapply(),我已经改变了

loop.vector <- c("rich_tot","rich_2m","rich_10m")
plot_list<-list()
for (i in loop.vector) {

 plot_list_rich<-list()
 plot_list_rich <- lapply(c("rich_tot", "rich_2m", "rich_10m"), function(i) {

最终代码:

plot_list_rich<-list()
plot_list_rich <- lapply(c("rich_tot", "rich_2m", "rich_10m"), function(i) {
  x <- as.data.frame(dataPDG[, c("Stations", i)])
  x[, 2] <- as.numeric(x[, 2])
p <- ggboxplot(x, x = "Stations", y = i, color = "Stations", add = "jitter", legend = "none") +
    rotate_x_text(angle = 45) +
    stat_compare_means(method = "anova", label.y = 13) +
    stat_compare_means(label = "p.signif", method = "anova", label.y = 12) 
max_values <- as.data.frame(max_rich[, c("Stations", i)])
hsd <- HSD.test(aov(x[, 2] ~ Stations, data = x), trt = "Stations", group = T)
sig.letter <- hsd$groups[order(row.names(hsd$groups)), ]
sig.letter <- sig.letter[c("JD", "ES", "SG", "CC", "AO", "BA", "BO", "BR", "FE", "SN", "GF", "GP"), ]
w <- max_values[[2]]
d <- p + geom_text(data = max_values, aes(x = Stations, y = 0.5 + w, label = sig.letter$groups), vjust = 0)
return(d)
})

谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.