Search code examples
rplotlyline-plot

Plot the line graph only for the non-zero columns using plot_ly() in R


I'm having a dataframe as follows

avgHours <- data.frame("QuarterYear" = c('2019 Q4','2020 Q1','2020 Q2'), average = c(44.5,44.5,42.0), leave = c(24.0,9.3,0.0), freeTime = c(0,0,0))

I need to plot 3 line graphs. In this case, I'm having a non-zero columns and 1 column with all the rows as 0. I need to omit that column while plotting.

This is the code for plotting the graph

    plot_ly(avgHours) %>%
      add_trace(x = avgHours$QuarterYear,
                y = avgHours$average,
                type = 'scatter',
                mode = 'lines+markers+text', 
                hovertext = paste('Quarter: ', avgHours$QuarterYear,
                                  '<br>Average hours per week: ', avgHours$average),                    
                line = list(color = 'rgb(242,142,43)', 
                            width = 3),
                marker = list(color = 'rgb(242,142,43)', 
                              size = 8),
                hoverinfo = "text", 
                text = avgHours$average,
                textposition = "top center",
                name = "Average Hours")%>%

      add_trace(x = avgHours$QuarterYear,
                y = avgHours$leave,
                type = 'scatter',
                mode = 'lines+markers+text', 
                hovertext = paste('Quarter: ', avgHours$QuarterYear,
                                  '<br>Average OOO hours per week: ', avgHours$leave),                    
                line = list(color = 'rgb(205, 12, 24)', 
                            width = 3),
                marker = list(color = 'rgb(205, 12, 24)', 
                              size = 8),
                hoverinfo = "text", 
                text = avgHours$leave,
                textposition = "top center",
                name = "Leave Average Hours")%>%

      add_trace(x = avgHours$QuarterYear,
                y = avgHours$freeTime,
                type = 'scatter',
                mode = 'lines+markers+text', 
                hovertext = paste('Quarter: ', avgHours$QuarterYear,
                                  '<br>Average Free Time per week: ', avgHours$freeTime),                    
                line = list(color = '#003049', 
                            width = 3),
                marker = list(color = '#003049', 
                              size = 8),
                hoverinfo = "text", 
                text = avgHours$freeTime,
                textposition = "top center",
                name = "Free Time Average Hours")%>%

      layout(
        yaxis = list(
          range = c(0,70),
          title = "Average Hours Per Week"
        ) 
      ) %>%
      layout(hoverlabel = list(bgcolor= 'white'), showlegend = TRUE)  %>%
      layout(yaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
      layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
      layout(legend = list(orientation = "h",
                           xanchor = "center",
                           x = 0.5,
                           y = -0.13))
    }

How can I plot the non-zero columns dynamically using plot_ly() in R?

Thanks in advance!!


Solution

  • You can have the add_trace in loop and specify the column selection by if condition as below:

    p <- plot_ly(data = avgHours)
    
    for(i in colnames(avgHours[-1])){
    
      if(sum(avgHours[[i]])>0){
    
        p <- p %>% 
          add_trace(x = avgHours[[1]],
                    y = avgHours[[i]],
                    type = 'scatter',
                    mode = 'lines+markers+text', 
                    hovertext = paste('Quarter: ', avgHours[[1]],
                                      '<br>Average hours per week: ', avgHours[[i]]),
                    line = list(width = 3),
                    marker = list(size = 8),
                    hoverinfo = "text",
                    text = avgHours[[i]],
                    textposition = "top center",
                    name = i)
      }
    }
    
    p <- p %>%
      layout(yaxis = list(
        range = c(0,70),
        title = "Average Hours Per Week")) %>%
      layout(hoverlabel = list(bgcolor= 'white'), showlegend = TRUE)  %>%
      layout(yaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
      layout(xaxis = list(showgrid = FALSE, zeroline = FALSE, showline = FALSE)) %>%
      layout(legend = list(orientation = "h", xanchor = "center", x = 0.5, y = -0.13))
    

    enter image description here

    If you want to have specific colours for each line and markers, you can add the list of colours and sub-setting it.