使用 Plotly 根据来自同一数据帧的列值绘制图形线 [英] Plotting graphs lines based on column values from the same datafram using Plotly
问题描述
我正在尝试在 ** R ** 上以 ** plotly ** 绘制每天销售的不同类型汽车的折线图.该图的外观是,它将包含每天销售的每种汽车的折线图.所以假设我有一个名为 ** df1 ** 的数据框
I am trying to plot a line graph of different types of cars sold per day in ** plotly ** on ** R **. The way the graph would look is that, it would have line graphs of each type of car that was sold on each day. So lets say I have the dataframe called ** df1 **
id date Value
Honda 10/30/12 2
Honda 10/31/12 3
Honda 11/1/12 3
Merc 11/2/12 4
Merc 10/30/12 1
Merc 10/31/12 2
Toyota 11/1/12 3
Toyota 11/3/12 2
现在我想在同一个 x 轴上设置三条线(每种类型的汽车一条线).
Now I want three lines(one line for each type of car) on the same x axis.
我尝试在 y 轴
参数中使用 filter()
函数,如下所示:
I tried using the filter()
function inside plotly in the y axis
argument as shown below:
plot_ly(df1,x=~date)%>%
add_trace(y=filter(df1,id=="Honda")$value,name="Honda",mode="lines+markers")%>%
add_trace(y=filter(df1,id=="Merc")$value,name="Honda",mode="lines+markers")%>%
add_trace(y=filter(df1,id=="Toyota")$value,name="Honda",mode="lines+markers")%>%
layout(xaxis=list(title="Date"),yaxis=list(title="Cars Sold"))
但是我得到了错误
错误:Tibble 列必须具有一致的长度,只有长度为 1 的值被回收:* 长度 3:列 y
* 长度 8:列 x
调用`rlang::last_erro()
Error: Tibble columns must have consistent lengths, only values of length one are recycled: * Length 3: Column
y
* Length 8: Columnx
Call `rlang::last_erro()
我知道它会抛出一个错误,因为 x 值更多而 y 值更少,我该如何应对?
I get that its throwing an error, due to having more x values while being given less y values, how do I cater it?
推荐答案
我没有看到同样的错误(没有指定跟踪类型,它绘制了一个直方图).
I didn't see the same error (without trace type specified, it drew a histogram).
library(plotly)
df1 <- data.frame(
id = c("Honda", "Honda", "Honda", "Merc", "Merc", "Merc", "Toyota", "Toyota"),
date = c('10/30/12', '10/31/12', '11/1/12', '11/2/12', '10/30/12', '10/31/12', '11/1/12', '11/3/12'),
Value = c(2, 3, 3, 4, 1, 2, 3, 2)
)
df1$date <- as.Date(df1$date, "%m/%d/%y")
plot_ly(df1,x=~date)%>%
add_trace(y=filter(df1,id=="Honda")$value,name="Honda",mode="lines+markers")%>%
add_trace(y=filter(df1,id=="Merc")$value,name="Honda",mode="lines+markers")%>%
add_trace(y=filter(df1,id=="Toyota")$value,name="Honda",mode="lines+markers")%>%
layout(xaxis=list(title="Date"),yaxis=list(title="Cars Sold"))
> plot_ly(df1,x=~date)%>%
+ add_trace(y=filter(df1,id=="Honda")$value,name="Honda",mode="lines+markers")%>%
+ add_trace(y=filter(df1,id=="Merc")$value,name="Honda",mode="lines+markers")%>%
+ add_trace(y=filter(df1,id=="Toyota")$value,name="Honda",mode="lines+markers")%>%
+ layout(xaxis=list(title="Date"),yaxis=list(title="Cars Sold"))
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Read more about this trace type -> https://plot.ly/r/reference/#histogram
您无需按每种汽车类型进行过滤.而是在调用 plot_ly 之前使用 group_by --- 也许这就是你的想法:
You don't need to filter by each car type. Instead use group_by before calling plot_ly --- perhaps this is what you had in mind:
df1 %>%
group_by(id) %>%
plot_ly(x=~date, y=~Value, type='scatter', color=~id, mode="lines+markers") %>%
layout(xaxis=list(title="Date"),yaxis=list(title="Cars Sold"))
这篇关于使用 Plotly 根据来自同一数据帧的列值绘制图形线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!