我正在使用 dabestr 绘制估计图,对于单个变量我成功了,但我想使用 for 循环创建一批图,但它不起作用。
library(dabestr)
plot(dabest(iris, Species, Petal.Width, idx = c("setosa", "versicolor"), paired = FALSE))
我想在 for 循环中绘制 Sepal.Length、Sepal.Width、Petal.Length、Petal.Width。 有人会帮忙吗?谢谢!
最佳答案
问题不在于 for 语句,而在于 dabest
函数。它仅接受.data
中给定的列名称,因此带有列名称的字符串不起作用...
经过一番挖掘,我发现了 this answer对于与变量名相关的 dplyr
问题非常有帮助。
library(dabestr)
library(ggpubr) # for ggarrange
to_plot <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
plots <- lapply(to_plot, function(co){
plot(dabest(iris, Species, UQ(rlang::sym(co)), idx = c("setosa", "versicolor"), paired = FALSE))
})
ggarrange(plotlist = plots, nrow = 2, ncol = 2)
请注意,UQ(rlang::sym(my_string))
在这里发挥了作用。这会产生以下图。
关于r - 如何在 dabestr 中创建绘图循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59158657/