Home > Software engineering >  how to use variable as column name in ggbetweenstats
how to use variable as column name in ggbetweenstats

Time:11-28

How could I use variable name in ggbetweenstats? None of these seem to work:

xField='Species'

ggbetweenstats(
  data = iris,
  x = xField,
  y = Sepal.Length,
  title = "Distribution of sepal length across Iris species"
)

ggbetweenstats(
  data =  iris,
  x = as.symbol(xField),
  y = Sepal.Length,
  title = "Distribution of sepal length across Iris species"
)

CodePudding user response:

Using rlang::sym and the bang-bang-operator (!!) you could do:

library(ggstatsplot)

xField <- "Species"

ggbetweenstats(
  data = iris,
  x = !!rlang::sym(xField),
  y = Sepal.Length,
  title = "Distribution of sepal length across Iris species"
)

  •  Tags:  
  • r
  • Related