【问题标题】:Plotly R chart from two different dataframe来自两个不同数据框的 Plotly R 图表
【发布时间】:2019-02-11 10:03:15
【问题描述】:

我正在尝试使用plotly 在 R 中使用两个不同的 data.frame 绘制折线图。问题是data.frame 的维度不同。 data.frame1 的样本数据:

                     DATE   SOC resdiff
 2016  2017-08-11 02:40:00 95.45    0.54
 4033  2017-08-18 02:45:00 94.88    0.56
 6048  2017-08-25 02:45:00 94.28    0.60
 8064  2017-09-01 02:45:00 93.68    0.60
 10080 2017-09-08 02:45:00 92.96    0.72
 12096 2017-09-15 02:45:00 92.13    0.83 

data.frame2 的示例数据:

       data.event_type data.user          data.stamp
 1          config    *INST* 2018-06-27 14:37:29
 2          config    *INST* 2018-02-14 19:30:57
 3          config    *SYNC* 2017-12-18 07:00:53
 4          config    *SYNC* 2017-12-18 06:59:14
 5          config    *INST* 2017-10-03 00:55:25
 6          config    *INST* 2017-09-28 00:49:29

data.frame 1 的折线图:

library(plotly)
p <- plot_ly(new_res, x = ~DATE) %>%
  add_trace(y = ~resdiff*100, name = 'SOC Diff',type = 'scatter',mode = 
 'lines') %>%
 add_trace(y = ~SOC, name = 'SOC',type = 'scatter', mode = 'lines+markers') 
p

data.frame 2 的折线图:

p <- plot_ly(get_df[1:9,], x = ~data.stamp) %>%
 add_trace(y = ~data.user, name = 'Event',type = 'scatter',mode = 'markers')
p

现在的问题是如何将这两个折线图组合成一个?是否可以通过添加add_trace来实现?

如果更容易,请随时建议我使用ggplot 或任何其他库。

【问题讨论】:

  • ggplot() + geom_line(aes(DATE, resdiff*100, color = 'a'), new_res) + geom_line(aes(data.stamp, data.user, color = 'b'), get_df[1:9,])?
  • 收到错误:Error: Discrete value supplied to continuous scale
  • @Axeman:感谢您的宝贵时间。我从这里得到了解决方案:plot.ly/r/graphing-multiple-chart-types
  • 是的,你在 y 上绘制了一个连续变量和离散变量,不知道它应该如何工作..
  • @Axeman:你是对的。我已经添加了参考答案。谢谢

标签: r ggplot2 r-plotly ggplotly


【解决方案1】:

最后,我从这里得到了解决方案:https://plot.ly/r/graphing-multiple-chart-types/

我合并了两个data.frame

final_df=merge(new_res,get_df[1:9,c(4,5,8)],all.x = TRUE,all.y = TRUE,by="DATE")

并使用plotly进行绘图:

library(plotly)

p <- plot_ly(final_df) %>%
 add_trace(x = ~DATE, y = ~SOC,name = 'SOC',type = 'scatter',mode = 'lines') %>%
 add_trace(x = ~DATE, y = ~resdiff*100, name = 'SOC Diff',type = 'scatter',mode = 'lines+markers') %>%
 add_trace(x = ~DATE, y = ~data.user, yaxis = 'y2',name = 'Event',type = 'scatter',mode = 'markers') %>% 
 layout(title = 'SOC Data',
     xaxis = list(title = ""),
     yaxis = list(side = 'left', title = 'SOC and SOC Diff', showgrid = FALSE, zeroline = FALSE),
     yaxis2 = list(side = 'right', overlaying = "y", title = 'Event Type', showgrid = FALSE, zeroline = FALSE))

p

【讨论】:

  • 是否可以在不合并两个数据帧的情况下做到这一点?我想从 8 个数据帧中添加数据,每个数据帧包含 10 列,合并它们会变得非常麻烦。
  • @AsmitaPoddar:由于这条线plot_ly(final_df),我不确定它只需要 data.frame 作为输入。到目前为止,我从未见过这样的例子,它需要多个 data.frame 作为输入。更多信息请参考此链接:plot.ly/r/graphing-multiple-chart-types
【解决方案2】:

我想在不合并数据帧的情况下做同样的事情。原来你可以dplyr这个人物本身。

# initialize plot
fig <- plot_ly()

# add data from first dataframe Df1
fig <- fig %>%
    add_markers(data=Df_blinks, name="line1", x = ~Timestamp, y = 0.3)

# add data from second dataframe Df2
fig <- fig %>%
    add_markers(data=Df_game, name="line2", x = ~Timestamp, y = ~CurrentRecognitionRate)

# show figure
fig

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多