Multiple line chart using plotly r

2019-08-13 18:26发布

问题:

I have a data frame which I am trying to plot using plotly as multiple line chart.Below is how the dataframe looks like:

  Month_considered pct.x pct.y   pct
   <fct>            <dbl> <dbl> <dbl>
 1 Apr-17            79.0  18.4  2.61
 2 May-17            78.9  18.1  2.99
 3 Jun-17            77.9  18.7  3.42
 4 Jul-17            77.6  18.5  3.84
 5 Aug-17            78.0  18.3  3.70
 6 Sep-17            78.0  18.9  3.16
 7 Oct-17            77.6  18.9  3.49
 8 Nov-17            77.6  18.4  4.01
 9 Dec-17            78.5  18.0  3.46
10 Jan-18            79.3  18.4  2.31
11 2/1/18            78.9  19.6  1.48

When I iterate through to plot multiple lines below is the code used.

colNames <- colnames(delta)
p <-
  plot_ly(
    atc_seg_master,
    x = ~ Month_considered,
    type = 'scatter',
    mode = 'line+markers',
    line = list(color = 'rgb(205, 12, 24)', width = 4)
  )

for (trace in colNames) {
  p <-
    p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}

p %>%
  layout(
    title = "Trend Over Time",
    xaxis = list(title = ""),
    yaxis = list (title = "Monthly Count of Products Sold")
  )
p

This is how the output looks like

My question is how to remove trace 0 and month_considered to remove from the chart even though its not in colnames which I loop through to add the lines.

回答1:

It looks like you were getting tripped up by two things:

  1. When you initially defined p and included the data and x arguments, a trace was created -- trace 0. You can define a plot without providing any data or x values to start by just using p <- plot_ly() along with any desired layout features.
  2. When you loop through the column names, your x axis column, Month_Considered is part of the set. You can exclude this by using setdiff() (part of base R) to create a vector with all of your column names except for Months_Considered

Putting those two things together, one way (of many possible) to accomplish what you're going for is as follows:

library(plotly)

df <- data.frame(Month_Considered = seq.Date(from = as.Date("2017-01-01"), by = "months", length.out = 12),
                 pct.x = seq(from = 70, to = 80, length.out = 12),
                 pct.y = seq(from = 30, to = 40, length.out = 12),
                 pct = seq(from = 10, to = 20, length.out = 12))


## Define a blank plot with the desired layout (don't add any traces yet)
p <- plot_ly()%>%
  layout(title = "Trend Over Time",
         xaxis = list(title = ""),
         yaxis = list (title = "Monthly Count of Products Sold") )

## Make sure our list of columns to add doesnt include the Month Considered
ToAdd <- setdiff(colnames(df),"Month_Considered")

## Add the traces one at a time
for(i in ToAdd){
  p <- p %>% add_trace(x = df[["Month_Considered"]], y = df[[i]], name = i,
                       type = 'scatter',
                       mode = 'line+markers',
                       line = list(color = 'rgb(205, 12, 24)', width = 4))
}

p