5

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
  • 11,106
  • 16
  • 64
  • 110

1 Answers1

7

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
  • 69,533
  • 24
  • 154
  • 197
drmariod
  • 11,106
  • 16
  • 64
  • 110