清晰标记水平barplot

清晰标记水平barplot

本文介绍了清晰标记水平barplot / geom_segment上的时间等级数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我需要显示每个医院病床的排空时间(通过患者出院)以及新病人被放置在床上的时间,并标记指示中间步骤为新患者准备病床。 / p>

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

问题:如何更好地在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(远程,远程,2P,2P,3P,远程,ICU),
DcOrderTime =结构(c(0.358333333333333,0.377777777777778,0.430555555555556,0.470833333333333,
format =h:m:s,class =times),
DcTime = structure(c(0.457638888888889,0.475694444444444,0.5,0.59375,0.625,0.700694444444444,0.155633633833833,0.623611111111111,0.4446666666666667)
format =h:m:s,class =times),
DecisionTime = structure(c(0.582638888888889,0.539583333333333,0.8886111111111111,0.596527777777778,0.675,0.66534 (c)(0.60625,0.545138888888889,0.91875,0.84375,0.707638888888889,0.713888888888889,0.857638888888889),
format =h:m:s,class =times ,
format =h:m:s,class =times),
DepartTime = structure(c(0.629166666666667,0.599305555555556,0.974305555555556,0.952083333333333,0.859722222222222,0.770138888888889,0.910416666666667),
格式=h:m:s,class =times)),
class =data.frame,row.names = c(NA,-7L),
.Names = c(Date,RoomBed,UnitGroup,DcOrderTime,DcTime,DecisionTime,RequestBedTime,DepartTime))

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



内置 scale_x_time 在这里似乎没有工作,这是导致我怀疑次类持有持续时间而不是一天中的时间的一部分。如果您经常使用这种格式,可能需要反复编写您自己的转换/缩放功能以便重复使用。


Background: I need to display the time each hospital bed is emptied (via patient discharge) and the time a new patient is placed in the bed, with markers indicating steps taken in between to prepare the bed for the new patient.

Plotting goals: My intention is to have two horizontal bars for each Room/Bed: one (currently in grey) showing the patient who's been discharged, and another (in blue) showing the new patient who's been placed in the same bed.

Question: How can I better display time on the x-axis?I'd be grateful for any referrals to documentation on working with class = "times" data, too.

Sample data:

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

And current plot, with times displayed as decimals:

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

When I view the BedMap df, the times show up formatted correctly (eg. DcOrderTime shows up as "08:36:00", "09:04:00", "10:20:00", "11:18:00", "09:30:00", etc.). But the x-axis is displayed as a decimal. Is there a way to just tell my plot code to display the x-axis as "times" class, without having to map the decimals to display values?

View(BedMap)
解决方案

The issue is, I think, linked to the fact that the times class appears to be holding durations rather than times of day (though I cannot actually find the documentation on the class, having never used it before). Because that format is not explicitly handled by ggplot it is being converted to numeric for plotting (you can see the stored numeric values in your output from dput).

While there is likely a better solution involving converting to POSIX or storing the values directly as time of day, you can accomplish your immediate goal by simply multiplying the numeric value (which is fraction of a day) by 24 to get the time. Then, set sensible axis breaks and add labels as desired, like so:

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

Which gives

The builtin in scale_x_time does not appear to work here, and that is part of what leads me to suspsect that the times class holds durations rather than times of day. If you are using this format often, it may be worthwhile to instead write your own transformation/scale function to use repeatedly.

这篇关于清晰标记水平barplot / geom_segment上的时间等级数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:05