本文介绍了R错误:第一个参数`data`必须是数据框或共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用R编程语言.我在这里关注本教程:
对于第二个情节:
fig<-plot_ly(df,x =〜date)图<-图%>%add_lines(y =〜y,name ="A")图<-图%>%add_lines(y =〜z,名称="B",可见= F)图<-图%>%布局(title =下拉菜单-样式",xaxis = list(domain = c(0.1,1)),yaxis = list(title ="y"),updatemenus =列表(列表(y = 0.8,按钮=列表(列表(方法=样式",args = list("line.color","blue"),标签=蓝色"),列表(方法=样式",args = list("line.color","red"),标签=红色")))))图
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
解决方案
I am not sure which plot from that page you are trying to implement. Here is a way to implement first 2 of them.
Data :
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)
Simple Dropdown :
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
For the 2nd plot :
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
这篇关于R错误:第一个参数`data`必须是数据框或共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!