2017-02-15 22 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)