I am trying to design a function where there are multiple inputs on what factors can control how a chart is colored. I am trying to use a character string to desegnate the column of the data.frame to use, but it is not working within the color =
paramater of the plotly statement. Many Thanks
Error in !as.name(desired_factor) : invalid argument type
require(dplyr)
require(plotly)
set.seed(42)
df <- data.frame(x = rep(LETTERS[1:5], 3),
y = rexp(15, rate = 0.5),
z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
df <- arrange(df, desc(z))
df$z2 <- c(rep("a", 5), rep("b", 5), rep("c", 5))
desired_factor <- "z2"
plot_ly(df,
x = ~x,
y = ~y,
color = ~factor(!!as.name(desired_factor)),
colors = "Blues",
type = "bar") %>%
layout(barmode = "stack")