使用变量像aes_string一样在r中选择轴
问题描述:
我试图提供一个带有列名的变量来创建一个 plotly
图,类似于 ggplot2::aes_string
.不知怎的,我被卡住了...
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.
答
刚刚通过阅读手册(一如既往)想通了...使用 as.formula
效果很好.
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?