ggplot2正在跳过x轴上的时间间隔

时间:2022-11-30 09:40:58

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.

我正在使用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数据。这是开始第一次转换的数据。

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() 

1 个解决方案

#1


2  

The way you were defining your times in as.POSIXct only took the hours, so the information on AM/PM got removed.

您在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

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.

如果切换format参数以给出关于时间列如何格式化的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

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").

注意我使用tz =“UTC”而不是让R使用我的本地时区。这是scale_x_datetime中的默认时区,如果我忘记这样做,我的所有时间都会在我的情节中得到偏移。另一种方法是在scale_x_datetime中设置date_format中的时区,例如date_format(“%I:%M%p”,tz =“America / Los_Angeles”)。

#1


2  

The way you were defining your times in as.POSIXct only took the hours, so the information on AM/PM got removed.

您在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

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.

如果切换format参数以给出关于时间列如何格式化的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

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").

注意我使用tz =“UTC”而不是让R使用我的本地时区。这是scale_x_datetime中的默认时区,如果我忘记这样做,我的所有时间都会在我的情节中得到偏移。另一种方法是在scale_x_datetime中设置date_format中的时区,例如date_format(“%I:%M%p”,tz =“America / Los_Angeles”)。