R Error: First argument, `data`, must be a data frame or shared data

R Error: First argument, `data`, must be a data frame or shared data

我正在使用 R 编程语言。我在这里关注本教程:https://plotly.com/r/dropdowns/

我尝试创建自己的数据和 运行 相同的过程:

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

但这会产生以下错误:

Error: First argument, `data`, must be a data frame or shared data.

我试图为这个“无花果”添加另一个情节

# 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

但这也行不通。

有人可以告诉我我做错了什么吗?

谢谢

我不确定您要执行的是该页面中的哪个图。这是实现其中前两个的方法。

数据:

library(plotly)
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)

简单的下拉菜单:

fig <- plot_ly(df, x = ~y, y = ~z)
fig <- fig %>% add_markers(marker = list(line = list(color = "black", width = 1)))
fig <- fig %>% layout(
  title = "Drop down menus - Plot type",
  xaxis = list(domain = c(0.1, 1)),
  yaxis = list(title = "y"),
  updatemenus = list(
    list(
      y = 0.8,
      buttons = list(
        
        list(method = "restyle",
             args = list("type", "scatter"),
             label = "Scatter"),
        
        list(method = "restyle",
             args = list("type", "histogram2d"),
             label = "2D Histogram")))
  ))

fig

对于第二个情节:

fig <- plot_ly(df, x = ~date)
fig <- fig %>% add_lines(y = ~y, name = "A")
fig <- fig %>% add_lines(y = ~z, name = "B", visible = F)
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