如何在R

时间:2019-04-01 21:19:59

标签: r ggplot2 lubridate

我有以下名为data.frame的{​​{1}}。

qq

我想绘制head(qq) my_year my_time 1 2004 20:08:04 2 2004 10:22:40 3 2004 15:55:26 4 2004 15:54:00 5 2004 07:29:26 6 2004 13:23:16 my_time(在这个玩具示例中只有一年)。

my_year

enter image description here

问题::如何强制ggplot(qq, aes(factor(my_year), as.POSIXct(my_time, format = "%H:%M:%S"))) + ggbeeswarm::geom_quasirandom()+ theme_bw() + xlab("")+ ylab("Time of the day")+ scale_y_datetime(breaks = "2 hour", date_labels= "%H:%M") 轴从0:00到24:00?在这种情况下,数据到达的时间不会超过23:00,但是在y轴显示的是0:00而不是24:00。

如果您这样做,也可以使用此数据重现此问题

y

以下是您想重现问题的数据。

scale_y_datetime(breaks = "1 hour", ...)

1 个答案:

答案 0 :(得分:2)

一种方法是退出ITime格式,并使用带有刻度的数字刻度,如小时:

qq %>%
  mutate(hour_num = my_time %>% as.integer() %>% `/` (60*60)) %>%
  ggplot(aes(factor(my_year), hour_num)) +
  ggbeeswarm::geom_quasirandom()+
  theme_bw() + xlab("")+
  ylab("Time of the day") +
  scale_y_continuous(breaks = 2*0:12,
                     labels = function(x) {paste0(floor(x),":00")})

enter image description here