Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R plotly multi line graph with markers size defined in column

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading