【问题标题】:r - How to Create loop for plotting in dabestr?r - 如何在 dabestr 中创建用于绘图的循环?
【发布时间】:2020-03-28 05:32:39
【问题描述】:

我正在使用 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。 有人会帮忙吗?谢谢!

【问题讨论】:

    标签: r loops for-loop plot


    【解决方案1】:

    问题不在于 for 语句,而在于 dabest 函数。它接受.data 中给出的列名,因此具有列名的字符串不起作用...

    经过一番挖掘,发现this answerdplyr变量名相关问题很有帮助。

    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)) 正在发挥作用。这产生了以下情节。

    【讨论】:

    • dplyr 相关的功能可能会在自动化操作/fors/lapplys 等中出现不可预测的行为。如果您将来有其他问题,请不要犹豫,寻找类似的问题。
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多