Home > Back-end >  Create a grouped bar chart with dates that are displayed as many times as they exist in date column
Create a grouped bar chart with dates that are displayed as many times as they exist in date column

Time:02-02

I need to create a grouped bar chart with traffic.sp and traffic.sp2 as the groups with different color. Also I need all the dates that exist more than once in my dataframe like "2020-06-26" to be displayed as many times they exist in the x-axis of plot and not just once.

date = c("2020-02-06", "2020-11-21", "2019-10-26", 
         "2020-09-20", "2020-01-11", "2019-09-15", "2020-08-03", "2019-02-05", 
         "2018-05-18", "2020-01-20", "2020-01-29", "2019-04-15", "2019-06-27", 
         "2017-11-29", "2017-12-01", "2019-04-04", "2017-11-28", "2018-11-29", 
         "2020-06-26", "2020-06-26") 
traffic.sp = c("28", "28", "20", 
                                                     "20", "22", "36", "36", "29", "0", "22", "23", "28", "28", "37", 
                                                     "26", "15", "39", "38", "22", "22")
traffic.sp2 = c("28", "28", "20", 
               "20", "22", "36", "36", "29", "0", "22", "23", "28", "28", "37", 
               "26", "15", "39", "38", "22", "22")
accident.description=c("right lane blocked", 
                       "two lanes blocked", "two lanes blocked", "right lane blocked", 
                       "right lane blocked", "one lane blocked", "right and center lane blocked", 
                       "right lane blocked", "road closed", "two lanes blocked", "right lane blocked", 
                       "right lane blocked", "one lane blocked", "right lane blocked", 
                       "right lane blocked", "right lane blocked", "right lane blocked", 
                       "two lanes blocked", "two lanes blocked", "two lanes blocked"
)
df<-data.frame(date,traffic.sp,traffic.sp2,accident.description)

fig <- plot_ly(df, x = ~date, y = ~traffic.sp, type = 'bar', name = 'traffic.sp',
               hovertext = paste(
                 "<br>Date :",
                 df$date,
                 "<br>accident description :",
                 df$accident.description,
                 paste("<br> traffic.sp:"),
                 df$traffic.sp
               ),
               hoverinfo = "text" )
fig <- fig %>% add_trace(y = ~traffic.sp2, name = 'traffic.sp2',
                         
                         hovertext = paste(
                           "<br>Date :",
                           df$date,
                           "<br>accident description :",
                           df$accident.description,
                           paste("<br> traffic.sp:"),
                           df$traffic.sp2
                         ),
                         hoverinfo = "text")
fig <- fig %>% layout(yaxis = list(title = 'Traffic speed'), barmode = 'group')

fig    

CodePudding user response:

You could do it this way where you are basically de-duplicating any duplicated dates by adding a trailing space to every subsequent duplicate.

library(tidyverse)
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
date = c("2020-02-06", "2020-11-21", "2019-10-26", 
         "2020-09-20", "2020-01-11", "2019-09-15", "2020-08-03", "2019-02-05", 
         "2018-05-18", "2020-01-20", "2020-01-29", "2019-04-15", "2019-06-27", 
         "2017-11-29", "2017-12-01", "2019-04-04", "2017-11-28", "2018-11-29", 
         "2020-06-26", "2020-06-26") 
traffic.sp = c("28", "28", "20", 
               "20", "22", "36", "36", "29", "0", "22", "23", "28", "28", "37", 
               "26", "15", "39", "38", "22", "22")
traffic.sp2 = c("28", "28", "20", 
                "20", "22", "36", "36", "29", "0", "22", "23", "28", "28", "37", 
                "26", "15", "39", "38", "22", "22")
accident.description=c("right lane blocked", 
                       "two lanes blocked", "two lanes blocked", "right lane blocked", 
                       "right lane blocked", "one lane blocked", "right and center lane blocked", 
                       "right lane blocked", "road closed", "two lanes blocked", "right lane blocked", 
                       "right lane blocked", "one lane blocked", "right lane blocked", 
                       "right lane blocked", "right lane blocked", "right lane blocked", 
                       "two lanes blocked", "two lanes blocked", "two lanes blocked"
)
df<-data.frame(date,traffic.sp,traffic.sp2,accident.description)

df <- df %>% 
  arrange(date) %>% 
  mutate(date2 = case_when(date == lag(date) ~ paste(date, " ", sep=""), TRUE ~ date), 
         fac = as.factor(date2))


fig <- plot_ly(df, x = ~fac, y = ~traffic.sp, type = 'bar', name = 'traffic.sp',
               hovertext = paste(
                 "<br>Date :",
                 df$date,
                 "<br>accident description :",
                 df$accident.description,
                 paste("<br> traffic.sp:"),
                 df$traffic.sp
               ),
               hoverinfo = "text" )
fig <- fig %>% add_trace(y = ~traffic.sp2, name = 'traffic.sp2',
                         
                         hovertext = paste(
                           "<br>Date :",
                           df$date,
                           "<br>accident description :",
                           df$accident.description,
                           paste("<br> traffic.sp:"),
                           df$traffic.sp2
                         ),
                         hoverinfo = "text")
fig <- fig %>% layout(yaxis = list(title = 'Traffic speed'), barmode = 'group')

#fig  

Created on 2022-02-01 by the enter image description here

  •  Tags:  
  • Related