反向ggplot时标

时间:2018-09-24 18:25:11

标签: r datetime ggplot2 lubridate

我正在创建一个时间表图,以显示每个时间段中有多少事件。我已经从lubridate计算了所有这些信息,并可以将其绘制在ggplot上。但是我需要反转/翻转轴,以使顶部显示8am,然后下降到5pm,而不是5pm到8am。

这是我最终结果的一小段当前的样子。只需颠倒时间顺序,它将是完美的。这是从上午8点到下午5点以及M-F。

enter image description here

我尝试使用scale_y_reverse,但这不起作用。

我看到了这篇文章,但是我仍然无法弄清楚如何使用它来解决我的情况:Reverse datetime (POSIXct data) axis in ggplot

样本数据(使用dput fn创建):

df <- structure(list(ID = c(4108L, 4165L, 1504L, 2570L, 1523L, 2556L, 
3224L, 1503L, 3220L, 837L), START_TIME = structure(c(1504267200, 
1504281600, 1504258200, 1504278000, 1504263600, 1504256400, 1504258200, 
1504274400, 1504258200, 1504256400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), END_TIME = structure(c(1504270799, 1504285199, 
1504263599, 1504281599, 1504268999, 1504259999, 1504263599, 1504279799, 
1504263599, 1504259999), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Day = structure(c(5L, 5L, 2L, 3L, 2L, 3L, 4L, 2L, 4L, 1L), .Label = c("M", 
    "T", "W", "R", "F"), class = "factor")), row.names = c(322L, 
351L, 112L, 188L, 125L, 179L, 298L, 111L, 294L, 8L), class = "data.frame")

我的代码:

library(ggplot2)
library(scales)

ggplot(df) +
    geom_rect(aes(xmin= 0, xmax= 2, ymin = START_TIME, ymax = END_TIME), 
              color = "#ffffff", 
              position="dodge") + 
    scale_y_datetime(date_breaks="1 hour", labels = date_format("%I:%M %p"), expand = expand_scale(.1)) + 
    theme_minimal() +
    theme(axis.ticks.x = element_blank(),
            axis.text.x = element_blank(),
            panel.grid = element_blank(),
            panel.grid.major.y = element_line(color = "#cccccc"),
            axis.title = element_blank()) +
    facet_wrap(~Day, nrow=1) 

2 个答案:

答案 0 :(得分:3)

我无法在日期时间范围内执行此操作,但是另一种方法是转换为数字范围并手动处理标签:

library(lubridate)
df %>% 
  mutate(stime = hour(START_TIME) + minute(START_TIME)/60,
         etime = hour(END_TIME) + minute(END_TIME)/60) %>%

  ggplot() +
  geom_rect(aes(xmin= 0, xmax= 2, ymin = stime, ymax = etime), 
            color = "#ffffff", 
            position="dodge") + 
  scale_y_reverse(breaks = 6:23, 
                     labels = c(paste0(6:11, ":00 AM"), 
                                "12:00 PM",
                                paste0(1:11, ":00 PM")), 
                     expand = expand_scale(.1)) + 
  theme_minimal() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(color = "#cccccc"),
        axis.title = element_blank()) +
  facet_wrap(~Day, nrow=1) 

enter image description here

答案 1 :(得分:1)

我不确定这是否是您想要的。使用您参考的问题中的方法,我预定义了y轴的中断,然后创建了反向日期转换程序(来自参考的问题)。

library(ggplot2)
library(scales)

#create reverse time transformer function
#https://stackoverflow.com/questions/43625341/reverse-datetime-posixct-data-axis-in-ggplot#43626186
c_trans <- function(a, b, breaks = b$breaks, format = b$format) {
  a <- as.trans(a)
  b <- as.trans(b)
  name <- paste(a$name, b$name, sep = "-")

  trans <- function(x) a$trans(b$trans(x))
  inv <- function(x) b$inverse(a$inverse(x))
  trans_new(name, trans, inverse = inv, breaks = breaks, format=format)
}
rev_date <- c_trans("reverse", "time")

#create breaks in 1 hour increment
#mybreaks<-seq(as.POSIXct("2017-09-01 9:00", tz="UTC"), as.POSIXct("2017-09-01 18:00:00", tz="UTC"), 3600)
mybreaks=seq(as.POSIXct("2017-09-01 4:00", tz="UTC"), length.out = 20, by=3600)


#use scale continuous 
# to use the default breaks remove breaks=mybreaks
ggplot(df) +
  geom_rect(aes(xmin= 0, xmax= 2, ymin = START_TIME, ymax = END_TIME), 
            color = "#ffffff", 
            position="dodge") + 
  scale_y_continuous(breaks=mybreaks, labels = date_format("%I:%M %p"), expand = expand_scale(.1), trans = rev_date) + 
  theme_minimal() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid = element_blank(),
        panel.grid.major.y = element_line(color = "#cccccc"),
        axis.title = element_blank()) +
  facet_wrap(~Day, nrow=1) 

enter image description here