'Create a grouped bar chart with dates that are displayed as many times as they exist in date column of my dataframe
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
