【问题标题】:ggplot2 is skipping a time interval on the x-axisggplot2 正在跳过 x 轴上的时间间隔
【发布时间】:2016-06-09 19:00:09
【问题描述】:

我在 Windows 10 64 位上使用 R 3.2.3 到 RStudio 版本 0.99.491...制作我的第一个 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() 

【问题讨论】:

  • ggplot 错误地绘制数据是不寻常的。您确定下午 2 点的数据显示在错误的位置吗?或者你确定问题只是下午 2 点?您的图表显示 4 月从上午 8 点开始超过 10 点,但数据在上午 8 点具有值 8 的 4 月。我的猜测是转换是问题,而不是ggplot。你确定你的time_ 列是正确的吗?
  • @Gregor,是的,哇,所有的数据都是错误的。我不知道为什么。
  • 也许共享dput(line_hour) 这样我们至少可以从同一个(复制/粘贴)位置开始。
  • 您定义time_ 的方式对我不起作用,因为它没有考虑上午/下午,因此时间与原始时间不符。我最终切换到as.POSIXct(paste(dat_on$time), origin = "7:00 AM", format = "%I:%M %p", tz = "UTC"),情节看起来不同/很好。
  • @aosmith 解决了这两个问题。我很乐意将其作为答案。

标签: r ggplot2


【解决方案1】:

您在as.POSIXct 中定义时间的方式只需要几个小时,因此上午/下午的信息已被删除。

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 参数以向 R 提供有关 time 列的格式设置的信息,则结果看起来更好,并且生成的图表似乎有意义。

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 中的默认时区,如果我忘记这样做,我的所有时间都会在我的情节中得到补偿。另一种方法是在scale_x_datetime 中设置date_format 中的时区,例如date_format("%I:%M %p", tz = "America/Los_Angeles")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多