简体   繁体   English

R plotly 多线定制 plot

[英]R plotly customization of multiple line plot

I have the following sample dataframe and I'm using Plotly in my R Shiny app to create a multi line plot: I have the following sample dataframe and I'm using Plotly in my R Shiny app to create a multi line plot:

df2=data.frame(DATE=as.Date(c('2022-01-04','2022-01-08','2022-02-06','2022-01-12','2022-02-15','2022-01-25','2022-02-08')), 
             QUANTITY=c(50,50,50,120,120,70,50), 
             PRICE=c(111,112, 104,90,93,98,105), TYPE=c('summer','summer','summer','summer','summer','winter','winter'),
             COLOR=c('blue','blue','blue','blue','blue','red','red'))


  plot_ly(df2, x = ~eval(parse(text='DATE')), 
                 y = ~eval(parse(text='PRICE')), color = ~eval(parse(text='COLOR')), connectgaps = TRUE) %>% add_lines()  

The three questions I have are the following:我的三个问题如下:

  1. why the color doesn't work?为什么颜色不起作用? I'm getting lines that are blue and green.我得到蓝色和绿色的线条。
  2. I would need to use column 'QUANTITY' to determine the thickness of the lines.我需要使用“数量”列来确定线条的粗细。 For example, one line should be given by quantity = 50 and color = blue and a different line (thicker) by quantity = 120 and color = blue.例如,一条线应该由数量 = 50 和颜色 = 蓝色和不同的线(更粗)由数量 = 120 和颜色 = 蓝色给出。
  3. Is it possible to have for example one line being line+markers and another line being only markers and pass this info as a column of the dataframe?是否可以让例如一行作为行+标记而另一行仅作为标记并将此信息作为 dataframe 的一列传递? Thanks.谢谢。

color defines a variable to group colors by; color定义了一个变量来分组 colors ; colors is for designating a specific color. colors用于指定特定颜色。

For example,例如,

plot_ly(df2, x = ~DATE, y = ~PRICE, 
        color = ~COLOR, colors = ~COLOR,
        type = "scatter", mode = "markers+lines")

在此处输入图像描述

If you wanted the line size by quantity, you would need separate traces.如果您想要按数量计算线大小,则需要单独的迹线。 If you wanted to include markers by color, you need to do that with separate traces.如果您想按颜色包含标记,则需要使用单独的跟踪来完成。 The following code does this.以下代码执行此操作。 However, there was only 1 red at QUANTITY 50, and only 1 red at QUANTITY 70. Therefore, despite calling for lines, there is no line using this example data.然而,在 QUANTITY 50 只有 1 个红色,在 QUANTITY 70 只有 1 个红色。因此,尽管调用了线路,但没有使用此示例数据的线路。

p <- plot_ly()

lapply(1:length(unique(df2$QUANTITY)),
       function(j){
         lapply(1:length(unique(df2$COLOR)),
                function(k){
                  cols <- unique(df2$COLOR)[k]
                  
                  if(cols == "blue"){
                    p <<- p %>% 
                      add_lines(data = df2 %>% filter(COLOR == cols,
                                                      QUANTITY == unique(QUANTITY)[j]),
                                x = ~DATE, y = ~PRICE, color = I(cols),
                                line = list(width = 3 * (unique(df2$QUANTITY)[j]/100)))
                  }
                  if(cols == "red"){
                    p <<- p %>% 
                      add_trace(data = df2 %>% filter(COLOR == cols,
                                                      QUANTITY == unique(QUANTITY)[j]),
                                x = ~DATE, y = ~PRICE, color = I(cols),
                                line = list(width = 3 * (unique(df2$QUANTITY)[j]/100)),
                                type = "scatter", mode = "markers+lines")
                  }
                })
       })
p

在此处输入图像描述

If I take your expand on the example data, then use this code, it looks more obvious.如果我对示例数据进行扩展,然后使用此代码,它看起来更明显。

df3 <- data.frame(rbind(df2, df2 %>% mutate(PRICE = PRICE + 10, DATE = DATE + 4)))
p <- plot_ly()
lapply(1:length(unique(df3$QUANTITY)),
       function(j){
         lapply(1:length(unique(df3$COLOR)),
                function(k){
                  cols <- unique(df3$COLOR)[k]
                  
                  if(cols == "blue"){
                    p <<- p %>% 
                      add_lines(data = df3 %>% filter(COLOR == cols,
                                                      QUANTITY == unique(QUANTITY)[j]),
                                x = ~DATE, y = ~PRICE, color = I(cols),
                                line = list(width = 3 * (unique(df3$QUANTITY)[j]/100)))
                  }
                  if(cols == "red"){
                    p <<- p %>% 
                      add_trace(data = df3 %>% filter(COLOR == cols,
                                                      QUANTITY == unique(QUANTITY)[j]),
                                x = ~DATE, y = ~PRICE, color = I(cols),
                                line = list(width = 3 * (unique(df3$QUANTITY)[j]/100)),
                                type = "scatter", mode = "markers+lines")
                  }
                })
       })
p

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM