创建一个分组条形图,其中显示的日期与它们在我的数据框的日期列中存在的次数一样多
Create a grouped bar chart with dates that are displayed as many times as they exist in date column of my dataframe
我需要创建一个分组条形图,其中 traffic.sp
和 traffic.sp2
作为具有不同颜色的组。此外,我需要在我的数据框中多次存在的所有日期,如 "2020-06-26"
显示它们在绘图的 x 轴中存在的次数,而不仅仅是一次。
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
你可以这样做,基本上 de-duplicating 任何重复的日期,方法是在每个后续重复项中添加尾随 space。
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
由 reprex package (v2.0.1)
创建于 2022-02-01
我需要创建一个分组条形图,其中 traffic.sp
和 traffic.sp2
作为具有不同颜色的组。此外,我需要在我的数据框中多次存在的所有日期,如 "2020-06-26"
显示它们在绘图的 x 轴中存在的次数,而不仅仅是一次。
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
你可以这样做,基本上 de-duplicating 任何重复的日期,方法是在每个后续重复项中添加尾随 space。
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
由 reprex package (v2.0.1)
创建于 2022-02-01