0
ggplot(DelaysTimeofDayTotal, aes(CRSDepTime)) + geom_point(aes(y = DepDelay, color = "DepDelay"))
+ geom_point(aes(y=ArrDelay, color = "ArrDelay")) 
+ scale_x_discrete(limits=(day.name)) 
+ labs(x= "Time of Day", y = "Delays in minutes") 
+ ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") 
+ theme(plot.title = element_text(face="bold"))

This is the output

enter image description here

This is the dataframe used (int) (num) (num) CRSDepTime integers represent HHMM

enter image description here

I am a newbie to R, I will appreciate any help thank you.

jerry
  • 21
  • 4

1 Answers1

0

You should define day.name as a factor variable and use breaks() to make the x axis readable.

Sample code:

library(ggplot2)

DelaysTimeofDayTotal$day.name= factor(DelaysTimeofDayTotal$day.name, levels= c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))

ggplot(DelaysTimeofDayTotal, aes(day.name, CRSDepTime)) + 
  geom_point(aes(y = DepDelay, color = "DepDelay"))+
  geom_point(aes(y=ArrDelay, color = "ArrDelay")) +
  scale_x_discrete(breaks=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) +
 labs(x= "Time of Day", y = "Delays in minutes", color="Delay Status") +
 ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") +
 theme(plot.title = element_text(face="bold"))+
  theme_minimal()

Plot:

enter image description here

Also, I would use rather geom_jitter() to visualise the overlapping points.

Sample code:

ggplot(DelaysTimeofDayTotal, aes(day.name, CRSDepTime)) + 
  geom_jitter(aes(y = DepDelay, color = "DepDelay"))+
  geom_jitter(aes(y=ArrDelay, color = "ArrDelay")) +
  scale_x_discrete(breaks=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) +
 labs(x= "Time of Day", y = "Delays in minutes", color="Delay Satus") +
 ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") +
 theme(plot.title = element_text(face="bold"))+
  theme_minimal()

Plot:

enter image description here

Sample data:

DelaysTimeofDayTotal<-structure(list(CRSDepTime = c(510, 511, 512, 514, 515, 516, 517, 
518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530
), DepDelay = c(-0.4529247, 4.4774775, -2.5608586, 1.9598214, 
-0.32222292, -3.86666667, 4.91714286, -4.1439271, 6.2290914, 
0.9113794, 2.4074074, 0.1282051, 1.1053494, 0.8019896, 0.69339187, 
-2, 2.1190476, 2.74566427, 4.2522819, 1.0220646), ArrDelay = c(-1.34935901, 
1.14414414, -0.67111557, -1.62053571, -2.6085016, -11.8666667, 
9.4, -4.131957, 7.10905807, -0.57737008, 0.38888889, 1.10256441, 
10.7990334, -5.47912288, -0.75862405, 8, -9.6190476, 4.943389978, 
7.19421907, -0.88228056), day.name = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 
6L), .Label = c("Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Saturday", "Sunday"), class = "factor")), spec = structure(list(
    cols = list(CRSDepTime = structure(list(), class = c("collector_double", 
    "collector")), DepDelay = structure(list(), class = c("collector_double", 
    "collector")), ArrDelay = structure(list(), class = c("collector_double", 
    "collector")), day.name = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x1ac39d58>, row.names = c(NA, 
-20L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))
Rfanatic
  • 2,224
  • 1
  • 5
  • 21