Reputation: 67
I am using R 3.2.3 through RStudio Version 0.99.491, on Windows 10 64bit...Making my first geom_line ggplot chart I thought I successfully navigagted the problem with the brute force of a newbie. Until, with help, I figured out the POSIXct problem, showing that the graph ticks skip the 02:00 PM interval on the x-axis, going straight to 03:00 PM interval, but with the 02:00 PM data. Here is the data to start the first transformation.
Here is the Graph
library(reshape2)
library(ggplot2)
library(scales)
myData_on <- melt(line_hour_on, id.vars = "time")
dat_on <- myData_on[myData_on$time != "Total",]
dat_on$time_ <- as.POSIXct(paste(dat_on$time),origin = "7:00 AM", format = "%H")
on_nov <- dat_on[dat_on$variable=="nov",]
ggplot(data=dat_on, aes(x=time_, y=value, group =variable, colour = variable)) +
geom_line(data = dat_on, size = 2, alpha = 0.75) +
geom_point(data = dat_on, size =3, alpha = 0.75) +
geom_line(data = on_nov, color = "black", size = 3, alpha = 0.60) +
geom_point(data = on_nov, color = "grey30", size = 6.5) +
geom_line(data = on_nov, color = "white", size = 1.5, alpha = 0.97) +
geom_point(data = on_nov, color = "white", size = 5, alpha = 0.97) +
geom_point(data = on_nov, color = "blue", size = 3, alpha = 0.25) +
scale_x_datetime(labels = date_format("%I:%M %p"), breaks = date_breaks("2 hour"))+
scale_colour_manual(values = c('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928'))+
ggtitle("Boarding the Bus Ridership November 2016") +
labs(x="Time",y="Count")+
theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0.5)) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))+
theme_fivethirtyeight()
Upvotes: 0
Views: 702
Reputation: 36086
The way you were defining your times in as.POSIXct
only took the hours, so the information on AM/PM got removed.
head(dat_on[,c(1, 4)], n = 10)
time time_
1 8:00 AM 2016-06-09 08:00:00
2 9:00 AM 2016-06-09 09:00:00
3 10:00 AM 2016-06-09 10:00:00
4 11:00 AM 2016-06-09 11:00:00
5 12:00 PM 2016-06-09 12:00:00
6 1:00 PM 2016-06-09 01:00:00
7 2:00 PM 2016-06-09 02:00:00
8 3:00 PM 2016-06-09 03:00:00
9 4:00 PM 2016-06-09 04:00:00
10 5:00 PM 2016-06-09 05:00:00
If you switch the format
argument to give R info on how the time
column is formatted the results look better and the resulting graph appears to make sense.
dat_on$time_ <- as.POSIXct(paste(dat_on$time),
origin = "7:00 AM", format = "%I:%M %p", tz = "UTC")
head(dat_on[,c(1, 4)], n = 10)
time time_
1 8:00 AM 2016-06-09 08:00:00
2 9:00 AM 2016-06-09 09:00:00
3 10:00 AM 2016-06-09 10:00:00
4 11:00 AM 2016-06-09 11:00:00
5 12:00 PM 2016-06-09 12:00:00
6 1:00 PM 2016-06-09 13:00:00
7 2:00 PM 2016-06-09 14:00:00
8 3:00 PM 2016-06-09 15:00:00
9 4:00 PM 2016-06-09 16:00:00
10 5:00 PM 2016-06-09 17:00:00
Notice I used tz = "UTC"
instead of having R use my local time zone. This is the default time zone in scale_x_datetime
and if I forget to do this all my times get offset in my plot. An alternative would be to set the time zone in date_format
in scale_x_datetime
, e.g., date_format("%I:%M %p", tz = "America/Los_Angeles")
.
Upvotes: 2