4

I am trying to plot a line chart using two different data.frame in R using plotly. The problem is both data.frame have a different dimension. Sample data for 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 

Sample data for 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

Line chart for 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

Line chart for 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

Now problem is How to combine these two line charts into a single one? Is it possible to do by adding the add_trace?

Please feel free to suggest me using ggplot or any other library if it will be easier.

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46

2 Answers2

5

Finally, I got the solution from here: https://plot.ly/r/graphing-multiple-chart-types/

I merged two data.frame

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

And plot using 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

enter image description here

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
  • 3
    Is it possible to do this without merging the two dataframes? I want to add data from 8 dataframes, each containing 10 columns and merging them will become very cumbersome. – Asmita Poddar Jun 13 '19 at 09:15
  • @AsmitaPoddar: Due to this line `plot_ly(final_df)`, I am not sure as it takes only data.frame as input. I have never seen so far such example where it takes multiple data.frame as input. Please refer this link for the more information: https://plot.ly/r/graphing-multiple-chart-types/ – Saurabh Chauhan Jun 13 '19 at 12:58
5

I wanted to do the same thing without merging dataframes. It turns out you can dplyr the figure itself.

# 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

Graph displaying a line plot and a marker plot from two separate dataframes.

Bastian
  • 620
  • 5
  • 14