I'm learning R. With the help of Maximilian Peters' answer, I wrote a custom function to make a bunch of plotly scatterplots. I want to label the x and y axis titles with the column names from those variables.
Here is the code:
library(plotly)
my_plot <- function(x, y, ...) {
plot_ly(y = y, x = x, ...) %>%
add_markers() %>%
layout(xaxis = list(title = deparse(substitute(x))),
yaxis = list(title = deparse(substitute(y))))
}
my_plot(y = mtcars$mpg, x = mtcars$disp)
This sets the xaxis title to "x", but I want it to be "disp".
I also tried this code:
my_plot <- function(data, x, y, ...) {
plot_ly(y = data[[y]], x = data[[x]], ...) %>%
add_markers() %>%
layout(xaxis = list(title = deparse(substitute(data[[x]]))),
yaxis = list(title = deparse(substitute(data[[y]]))))
}
my_plot(data = mtcars, y = 'mpg', x = 'disp')
This sets the xaxis title to "data[[x]]".
Oops, I posted too quick. The solution was simple.
my_plot <- function(data, x, y, ...) {
plot_ly(y = data[[y]], x = data[[x]], ...) %>%
add_markers() %>%
layout(xaxis = list(title = x),
yaxis = list(title = y))
}
my_plot(data = mtcars, y = 'mpg', x = 'disp')