0

I want to produce something like this (this is for Python): https://i.stack.imgur.com/QCpIF.png

but I want to do it for R. This is my code in R:

mean_delays_day <- aggregate(cbind(ArrDelay, DepDelay) ~ DayOfWeek, data = df, FUN = mean)

bestday <- ggplot(mean_delays_day, aes(x = DayOfWeek)) + 
        geom_bar(aes(y = ArrDelay, fill = "ArrDelay"), stat = "identity", width = 0.7, alpha = 1) + 
        geom_bar(aes(y = DepDelay, fill = "DepDelay"), stat = "identity", position = "dodge", width = 0.7, alpha = 0.3) +
        scale_fill_manual(values = colors, name = "") +
        labs(title = "Average Delay by Day of the Week", x = "Day of the Week", y = "Average Delay Time (min)") +
        theme(plot.title = element_text(hjust = 0.5))

bestday

But the output is like this instead:

https://i.stack.imgur.com/rXPfM.png

Please help me, I'm still very much a beginner just trying to do an assignment for school.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • As is often the case, you fix this quite easily by reshaping your data to long format, i.e. try `mean_delays_day %>% pivot_longer(c(ArrDelay, DepDelay), names_to = "type", values_to "value") %>% ggplot(aes(x = DayOfWeek)) + geom_col(aes(y = value, fill = type), width = 0.7, position = "dodge")`. – stefan Mar 19 '23 at 11:16

0 Answers0