在情节上指示事件

时间:2019-04-04 13:24:42

标签: r

我正在研究患者的液体摄入量和排尿频率。

我正在收集液体的量和喝的时间以及排尿的时间。

我想在排尿时出现液体摄入的图表上指出。

到目前为止,这是我的数据和代码...

time_log <- c("01:10", "05:50", "06:00","06:15", "06:25", "09:35", "10:00", "12:40",
              "14:00")
time_log <- paste("04/04/2019", time_log, sep=" ")
time_log <- strptime(time_log, format = "%d/%m/%Y %H:%M")
time_view <- format(time_log, "%H:%M")
event <- c("u", "u", "T", "T", "u", "u", "T","T","u")
Volume <- c(NA, NA, 0.25, 0.25, NA, NA, 0.125, 0.625, NA)

patient_data <- data.frame(time_log, time_view, event, Volume)

total_liquids <- sum(patient_data$Volume, na.rm=TRUE)

plot(patient_data$time_log, patient_data$Volume,
     xlim = c(as.POSIXct("2019-04-04 00:00:00"),as.POSIXct("2019-04-04 24:00:00")), 
     xlab="Hours of Study", ylab = "Volume of Liquid Drank /L",
     main = paste("Total Liquids Drank = ", total_liquids, " L"))

这与以下问题有关 Time Series Data - How to,但Stack Overflow社区对此并不满意。

1 个答案:

答案 0 :(得分:2)

这是使用ggplot2和垂直虚线的一种方法。当添加geom_vline时,我们仅针对排尿事件(即event == "u")对数据进行子集化。

library(ggplot2)

ggplot(patient_data, aes(x = time_log, y = Volume)) +
  geom_point() +
  geom_vline(
    data = subset(patient_data, event == "u"), 
    aes(xintercept = time_log),
    linetype = 2
  ) +
  labs(
    title = paste("Total Liques Drank = ", total_liquids, " L"),
    subtitle = "Dashed line reprents urination",
    x = "Hours of Study",
    y = "Volume of Liquid Drank (L)"
  ) +
  scale_y_continuous(limits = c(0, NA)) # just so we don't start the y-axis at 0.1 or something misleading.

Visual