2

I am using dabestr for plotting estimation plots, for a single variable I succeed, but I want to create a batch of plots with the for loop and it not work.

library(dabestr)
plot(dabest(iris, Species,  Petal.Width, idx = c("setosa", "versicolor"), paired = FALSE))

I want to plot Sepal.Length, Sepal.Width, Petal.Length, Petal.Width in a for loop. Would anyone help? Thank you!

jeb
  • 78,592
  • 17
  • 171
  • 225
Runbin Sun
  • 45
  • 1
  • 4

1 Answers1

2

The issue is not with the for statement, it's with the dabest function. It's made to only accept column names as given in .data so a string with the column name doesn't work...

After digging a bit, I found this answer very helpful for dplyr related problems with variable names.

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)

Note that UQ(rlang::sym(my_string)) is doing the magic here. This yields the following plot.

enter image description here

RoB
  • 1,833
  • 11
  • 23
  • dplyr-related functions can behave unpredictably with automated actions / fors /lapplys, etc. don't hesitate to look around for similar problems if you have other issues in the future. – RoB Dec 03 '19 at 14:31