4

I am trying to provide a variable with the column name to create a plotly graph, similar to ggplot2::aes_string. Somehow I am stuck...

plot_ly(iris, x=~Sepal.Length, y=~Sepal.Width) # works as expected 
plot_ly(iris, x=~'Sepal.Length', y=~'Sepal.Width') # fails since it doesn't use the column

I guess this is a pretty easy question, but I just miss the vocabulary to find a solution on stack overflow.

drmariod
  • 10,480
  • 12
  • 53
  • 107

1 Answers1

5

Just figured it out by reading manual (as always)... Using as.formula works well.

my_x <- 'Sepal.Length'
my_y <- 'Sepal.Width'
plot_ly(iris, x=as.formula(paste0('~', my_x)), y=as.formula(paste0('~', my_y)))

But maybe there is a nicer solution?

Roman Luštrik
  • 67,056
  • 24
  • 151
  • 191
drmariod
  • 10,480
  • 12
  • 53
  • 107