This sounds like a popular plot but I really was trying to figure it out without any solution! Can I produce a plot that shows the percentage of the occurrence in each Blocked lanes inside each Duration? My data is
data<- structure(list(Lanes.Cleared.Duration = c(48, 55, 20, 38, 22,
32, 52, 21, 39, 14, 69, 13, 14, 13, 25), Blocked.Lanes = c(1L,
2L, 1L, 2L, 5L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 1L), Durations = structure(c(3L,
3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 4L, 2L, 2L, 2L, 2L), .Label = c("<10",
"<30", "<60", "<90", "<120", ">120"), class = "factor")), row.names = c(NA,
-15L), na.action = structure(c(`17` = 17L, `26` = 26L, `28` = 28L,
`103` = 103L, `146` = 146L, `166` = 166L, `199` = 199L, `327` = 327L,
`368` = 368L, `381` = 381L, `431` = 431L, `454` = 454L, `462` = 462L,
`532` = 532L, `554` = 554L, `703` = 703L, `729` = 729L, `768` = 768L,
`769` = 769L, `785` = 785L, `970` = 970L, `1043` = 1043L, `1047` = 1047L,
`1048` = 1048L, `1081` = 1081L, `1125` = 1125L), class = "omit"), class = "data.frame")
I tried the following code and it gave me Real Duration rather than percentage. Here is my code.
data %>%
ggplot(aes(fill=factor(Blocked.Lanes), y=Lanes.Cleared.Duration, x=Durations))
geom_bar(position="dodge", stat="identity")
My result should show the percentage of occurrence of each Blocked lane inside each Duration. I tried to group by Durations but it did not work.
CodePudding user response:
Not quite elegant, but you can do a tally by duration and blocked lane first, and then do a percentage with grouped duration.
df1 <- data %>% group_by(Durations, Blocked.Lanes) %>% tally()
df1 <- df1 %>% ungroup %>% group_by(Durations) %>% mutate(perc = n/sum(n))
ggplot(df1, aes(fill=factor(Blocked.Lanes), y=perc, x=Durations))
geom_bar(position="dodge", stat="identity")
CodePudding user response:
You can do:
library(tidyverse)
data %>%
count(Durations, Blocked.Lanes) %>%
group_by(Durations) %>%
mutate(n = prop.table(n) * 100) %>%
ggplot(aes(fill = factor(Blocked.Lanes), y = n, x = Durations))
geom_bar(position = "dodge", stat = "identity")
ylab("Percentage of Blocked Lane")
guides(fill = guide_legend(title = "Blocked Lane"))
Output