I am using the R programming language. I am following this tutorial over here: https://plotly.com/r/dropdowns/
I tried to create my own data and run the same procedure:
library(plotly)
library(MASS)
library(dplyr)
# create data
x <- sample( LETTERS[1:4], 731, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
y <- rnorm(731,10,10)
z <- rnorm(731,5,5)
date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
df <- data.frame(x,y, z, date)
df$x = as.factor(df$x)
#create plot
fig <- plot_ly(df, x = ~y, z = ~z )
fig <- fig %>% plot_ly(df, y = ~y, color = ~x, type = "box")
fig <- fig %>% plot_ly( data = df, type = "scatter", mode = "markers", x = ~ y, y = ~z)
fig <- fig %>% layout(
title = "Drop down menus - Styling",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.8,
buttons = list(
list(method = "restyle",
args = list("line.color", "blue"),
label = "Blue"),
list(method = "restyle",
args = list("line.color", "red"),
label = "Red")))
)
)
fig
But this produces the following error:
Error: First argument, `data`, must be a data frame or shared data.
I tried to add another plot to this "fig"
# time series plot
aggregate = df %>%
mutate(date = as.Date(date)) %>%
group_by(month = format(date, "%Y-%m")) %>%
summarise( mean = mean(y))
ts_1 <- ggplot(aggregate) + geom_line(aes(x = month, y = mean, group = 1)) + theme(axis.text.x = element_text(angle = 90)) + ggtitle("time series 1")
plot_1 = ggplotly(ts_1)
fig <- fig %>% plot_1
But this also does not work.
Can someone please show me what I am doing wrong?
Thanks