Home > Enterprise >  R plotly multi line graph with markers size defined in column
R plotly multi line graph with markers size defined in column

Time:09-17

I have the following code that is supposed to show a multi-line graph with both lines and markers:

import(plotly)
df = data.frame(DATE = c('2022-09-01','2022-09-02','2022-09-03','2022-09-04', '2022-09-01','2022-09-02','2022-09-03','2022-09-04'),
                      SIZE = c(200,200,180, 110, 20, 15, 15, 10),
                      PRICE = c(99, 95.5, 96, 96, 80, 79, 77, 77),
                      ID = c('ABC','ABC','ABC','ABC','DEF','DEF','DEF','DEF'), stringsAsFactors = F)
  
  df$DATE = as.Date(df$DATE)
  
  plt <- df %>% plot_ly( x = ~eval(parse(text='DATE')), 
                         y = ~eval(parse(text='PRICE')), 
                         color = ~eval(parse(text='ID')),
                         name = ~eval(parse(text='ID')), 
                         connectgaps = TRUE) %>% add_lines()  %>% add_markers()

It works ok but my goal is to have each marker size defined through column "SIZE". Is there any way to accomplish this? I couldn't find a similar question. Thanks

CodePudding user response:

Adding size = ~SIZE, seems to work well.
Is it what you were searching for?

df %>% plot_ly( x = ~eval(parse(text='DATE')), 
                       y = ~eval(parse(text='PRICE')), 
                       size = ~SIZE,
                       color = ~eval(parse(text='ID')),
                       name = ~eval(parse(text='ID')), 
                       connectgaps = TRUE) %>% add_lines()

enter image description here

  • Related