在水平条形图/ geom_segment上清除时间类数据的标签

时间:2017-02-15 19:03:39

标签: r ggplot2 time-series

背景:我需要显示每个病床被清空的时间(通过患者出院)和新病人被放置在床上的时间,并且标记指示为新患者准备床之间所采取的步骤。 / p>

绘图目标:我的目的是为每个房间/床铺设两个水平条:一个(目前为灰色)显示已经出院的患者,另一个(蓝色)显示新患者&#39被放在同一张床上。

问题:如何更好地在x轴上显示时间? 我很感激任何关于使用class =" times"的文档的推荐。数据也是。

示例数据:

BedMap <- structure(list(
  Date = structure(c(17210, 17210, 17210, 17210, 17210, 17210, 17210), class = "Date"), 
  RoomBed = c("TestBed1", "TestBed2", "TestBed3", "TestBed4", "TestBed5", "TestBed6", "TestBed7"), 
  UnitGroup = c("Tele", "Tele", "2P", "2P", "3P", "Tele", "ICU"), 
  DcOrderTime = structure(c(0.358333333333333, 0.377777777777778, 0.430555555555556, 0.470833333333333, 0.395833333333333, 0.623611111111111, 0.441666666666667), 
                      format = "h:m:s", class = "times"), 
  DcTime = structure(c(0.457638888888889, 0.475694444444444, 0.5, 0.59375, 0.625, 0.700694444444444, 0.770833333333333), 
                 format = "h:m:s", class = "times"), 
  DecisionTime = structure(c(0.582638888888889, 0.539583333333333, 0.886111111111111, 0.596527777777778, 0.675, 0.653472222222222, 0.777777777777778), 
                       format = "h:m:s", class = "times"), 
  RequestBedTime = structure(c(0.60625, 0.545138888888889, 0.91875, 0.84375, 0.707638888888889, 0.713888888888889, 0.857638888888889), 
                         format = "h:m:s", class = "times"),
  DepartTime = structure(c(0.629166666666667, 0.599305555555556, 0.974305555555556, 0.952083333333333, 0.859722222222222, 0.770138888888889, 0.910416666666667), 
                     format = "h:m:s", class = "times")), 
  class = "data.frame", row.names = c(NA, -7L), 
  .Names = c("Date", "RoomBed", "UnitGroup", "DcOrderTime", "DcTime", "DecisionTime", "RequestBedTime", "DepartTime"))

当前情节,时间显示为小数:

enter image description here

ggplot(BedMap) +
  geom_segment(aes(x = 0, #start of light grey bar, indicates bed occupied & discharge planning has not yet started
               y = RoomBed, 
               xend = DcOrderTime, #end of light grey bar
               yend = RoomBed), 
           color = "Light Grey", 
           size = 6) +
  geom_segment(aes(x = DcOrderTime, #start of dark grey bar, indicates bed still occupied but discharge planning has begun
               y = RoomBed, 
               xend = DcTime, #end of dark grey bar, indicates patient has been discharged, bed is unoccupied
               yend = RoomBed), 
               color = "Dark Grey", 
               size = 6) +
  geom_segment(aes(x = DepartTime, #start of blue bar, indicates new patient has been placed in bed
               y = RoomBed, 
               xend = 1, #end of blue bar (set at midnight/end of day)
               yend = RoomBed), 
               color = "Blue", 
               size = 6) +
  geom_point(aes(x = DecisionTime, 
             y = RoomBed), 
         color = "black", size = 3) +
  geom_point(aes(x = RequestBedTime, 
             y = RoomBed), 
         color = "green", size = 3) +
  theme(legend.position = "none") +
  labs(x = "Time", y = "Room-Bed", title = "Daily Bed Map")

当我查看BedMap df时,时间显示格式正确(例如,DcOrderTime显示为&#34; 08:36:00&#34;,&#34; 09:04:00&#34;,& #34; 10:20:00&#34;,&#34; 11:18:00&#34;,&#34; 09:30:00&#34;等)。但x轴显示为小数。有没有办法告诉我的情节代码将x轴显示为&#34;次&#34; class,而不必映射小数来显示值?

View(BedMap)

2 个答案:

答案 0 :(得分:1)

我认为,这个问题与unset($array[array_search("b",$array)]); 类似乎持有持续时间而不是一天中的事实有关(尽管我实际上找不到类的文档,以前从未使用过它) 。由于times未明确处理该格式,因此将其转换为ggplot进行绘图(您可以在numeric的输出中看到存储的数值)。

虽然可能有一个更好的解决方案,包括转换为dput或直接将值存储为时间,但您可以通过简单地乘以POSIX值(它的一小部分)来实现您的直接目标那天)24点来得到时间。然后,设置合理的轴断点并根据需要添加标签,如下所示:

numeric

哪个给出了

enter image description here

ggplot(BedMap) + geom_segment(aes(x = 0, #start of light grey bar, indicates bed occupied & discharge planning has not yet started y = RoomBed, xend = 24*DcOrderTime, #end of light grey bar yend = RoomBed), color = "Light Grey", size = 6) + geom_segment(aes(x = 24*DcOrderTime, #start of dark grey bar, indicates bed still occupied but discharge planning has begun y = RoomBed, xend = 24*DcTime, #end of dark grey bar, indicates patient has been discharged, bed is unoccupied yend = RoomBed), color = "Dark Grey", size = 6) + geom_segment(aes(x = 24*DepartTime, #start of blue bar, indicates new patient has been placed in bed y = RoomBed, xend = 24, #end of blue bar (set at midnight/end of day) yend = RoomBed), color = "Blue", size = 6) + geom_point(aes(x = 24*DecisionTime, y = RoomBed), color = "black", size = 3) + geom_point(aes(x = 24*RequestBedTime, y = RoomBed), color = "green", size = 3) + theme(legend.position = "none") + labs(x = "Time", y = "Room-Bed", title = "Daily Bed Map") + scale_x_continuous(breaks = seq(0, 24, 3) , labels = sprintf("%02d:00",seq(0, 24, 3)) ) 中的内置似乎不起作用,这是导致我怀疑scale_x_time类持有持续时间而不是一天中的时间的一部分。如果您经常使用这种格式,那么可能值得编写您自己的转换/缩放函数以便重复使用。

答案 1 :(得分:0)

知道了!事实证明,class =“times”来自chron包。通过在我的绘图末尾添加一条简单的线条,显示更改简单,无需额外的映射。请参阅下面的原始代码,以及其他最后一行。

ggplot(BedMap) +
    geom_segment(aes(x = 0, #start of light grey bar, indicates bed occupied & discharge planning has not yet started
           y = RoomBed, 
           xend = DcOrderTime, #end of light grey bar
           yend = RoomBed), 
       color = "Light Grey", size = 6) +
    geom_segment(aes(x = DcOrderTime, #start of dark grey bar, indicates bed still occupied but discharge planning has begun
           y = RoomBed, 
           xend = DcTime, #end of dark grey bar, indicates patient has been discharged, bed is unoccupied
           yend = RoomBed), 
           color = "Dark Grey", size = 6) +
    geom_segment(aes(x = DepartTime, #start of blue bar, indicates new patient has been placed in bed
           y = RoomBed, 
           xend = 1, #end of blue bar (set at midnight/end of day)
           yend = RoomBed), 
           color = "Blue", size = 6) +
    geom_point(aes(x = DecisionTime, 
         y = RoomBed), 
     color = "black", size = 3) +
    geom_point(aes(x = RequestBedTime, 
         y = RoomBed), 
     color = "green", size = 3) +
    theme(legend.position = "none") +
    labs(x = "Time", y = "Room-Bed", title = "Daily Bed Map") +
    scale_x_chron(format = "%H:%m", n = 12)