我正在使用R 3.2.3通过RStudio版本0.99.491,在Windows 10 64bit上...制作我的第一个geom_line ggplot图表我认为我成功地用新手的蛮力导航问题。在帮助之前,我想出了POSIXct问题,显示图表标记在x轴上跳过02:00 PM间隔,直接到03:00 PM间隔,但是在02:00 PM数据。 这是开始第一次转换的data。
以下是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()
答案 0 :(得分:2)
您在as.POSIXct
中定义时间的方式只花了几个小时,因此有关AM / PM的信息被删除了。
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
如果您切换format
参数以提供有关time
列格式化方式的R信息,结果会更好看,结果图表似乎有意义。
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
注意我使用tz = "UTC"
而不是让R使用我的本地时区。这是scale_x_datetime
中的默认时区,如果我忘记这样做,我的所有时间都会在我的情节中得到偏移。另一种方法是在date_format
scale_x_datetime
中设置时区,例如date_format("%I:%M %p", tz = "America/Los_Angeles")
。