2017-02-15 46 views
0

背景:我需要显示一个新的病人被放置在床中的每个时间病床被排空的时间(通过患者放电),并与指示所采取的步骤的标记为新患者准备床。上水平barplot倍类数据的清除标记/ geom_segment

绘图目标:我的目的是为每个房间/床铺设置两个单杠:一个(当前为灰色)显示已经出院的患者,另一个(蓝色)显示已经放置在同一位置的新患者床。

问题:如何更好地在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东风,与时俱进正确显示格式(例如DcOrderTime显示为“08:36:00”,“09:04:00”,“10:20:00”,“11:18:00”,“09:30:00”等)。但x轴显示为小数。有没有办法让我的情节代码将x轴显示为“times”类,而不必将小数映射到显示值?

View(BedMap) 
+0

什么是你的时间价值?你想如何展示它们?编写一个将时间值转换为所需格式的函数,然后参见http://stackoverflow.com/questions/11610377/how-do-i-change-the-formatting-of-numbers-on-an-axis-与-ggplot的方式来使用它。 –

+0

当我使用View(BedMap)查看当前数据时,如上所示,时间值显示为次数,例如:DcOrderTime < - c(“08:36:00”,“09:04:00”,“10 :20:00“,”11:18:00“,”09:30:00“,”14:58:00“,”10:36:00“)。我知道如何在轴上映射一个随机标签,但我认为数据已经是时间格式,我不需要做任何转换。我认为可能有一种方法来指定轴应该以h:m:s格式显示。 – jesstme

+1

'times'类定义在哪里? –

回答

1

的问题是,我认为,联系到了times类似乎是抱着持续时间,而不是一天的时间的事实(虽然我不能真正找到在类的文档,从来没有使用过它) 。由于ggplot未明确处理该格式,因此将其转换为numeric进行绘图(您可以在dput的输出中看到存储的数值)。

虽然可能有更好的解决方案,包括转换为POSIX或直接将值存储为一天中的时间,但您可以通过将numeric值(一天的几分之一)乘以24来获得时间。然后,设置合理的轴断裂,并根据需要添加标签,就像这样:

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

其中给出

enter image description here

scale_x_time的内建不会出现在这里工作,那就是部分是什么导致我怀疑times课程的持续时间而不是一天的时间。如果您经常使用这种格式,可能需要重复编写您自己的转换/缩放功能。

0

Got it!原来,答案在于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)