【问题标题】:X-axis for timesX轴时间
【发布时间】:2019-04-05 15:48:37
【问题描述】:

我正在尝试生成一系列图表,显示同一位患者在不同时间喝酒和小便。每个图代表一天。我想比较这些日子,因此我需要确保绘制的所有图表都具有相同的 x 轴。我的代码在下面,我抄自How to specify the actual x axis values to plot as x axis ticks in R

### Data Input

time_Thurs <- c("01:10", "05:50", "06:00","06:15", "06:25", "09:35", "10:00", "12:40",
              "14:00", "17:20", "18:50", "19:10", "20:10", "21:00", "22:05", "22:35")
event_Thurs <- c("u", "u", "T", "T", "u", "u", "T","T","u", "u", "T", "T", "T", "T", "u", "W")
volume_Thurs <- c(NA, NA, 0.25, 0.25, NA, NA, 0.125, 0.625, NA, NA, 0.25, 0.25, 0.25, 0.25,
            NA, 0.25)
total_liquids_Thurs <- sum(volume_Thurs, na.rm=TRUE)
time_Thurs <- paste("04/04/2019", time_Thurs, sep=" ")

time_Fri <- c("01:15", "06:00", "06:10", "06:25", "06:30", "07:10", "08:40", "09:20",
              "12:45", "13:45")
event_Fri <- c("u","u", "T","T","u","uu","T", "u", "T", "u")
volume_Fri <- c(NA, NA, 0.25, 0.25, NA, NA, 0.125, NA, 0.625, NA)
total_liquids_Fri <- sum(volume_Fri, na.rm=TRUE)
time_Fri <- paste("05/04/2019", time_Fri, sep=" ")

### Collect all data together

event <- c(event_Thurs, event_Fri)
Volume <- c(volume_Thurs, volume_Fri)
time_log <- c(time_Thurs, time_Fri)
time_log <- strptime(time_log, format = "%d/%m/%Y %H:%M")
time_view <- format(time_log, "%H:%M")

### Put into Dataframe

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

# write.csv(patient_data, file="patient_data.csv", row.names = FALSE)

daily_plot <- function(x, day) {
  #    x patient data - a data.frame with four columns:
  #    POSIXct time, time, event and Volume
  #    date number of day of month
  #    y volume of liquid
  #    TotVol total volume of intake over week
  #    Event - drink or otherwise

  x <- x[as.numeric(format(x[,1], "%d")) == day, ]
  TotVol <- sum(x[,4], na.rm = TRUE)
  DayOfWeek <- weekdays(x[1,1], abbreviate = FALSE)

  plot(x[,1],x[,4], 
       xlim = c(x[1,1],x[length(x[,1]),1]),
       xlab="Hours of Study", ylab = "Volume of Liquid Drank /L",
       main = paste("Total Liquids Drank = ", TotVol, " L on ", DayOfWeek, "Week 1, Apr 2019"),
       sub = "dashed red line = urination", pch=16,
       col = c("black", "yellow", "green", "blue")[as.numeric(x[,3])],
       xaxt = 'n'
  )
  xAxis_hrs <- seq(as.POSIXct(x[1,1]), as.POSIXct(x[length(x[,1]),1]), by="hour")
  axis(1, at = xAxis_hrs, las = 2)
  abline( v = c(x[x[,3] == "u",1]), lty=3, col="red")
}

当我运行函数时,

daily_plot(patient_data, 4)

我想打印出我的 x 轴,它以小时的形式修正,代表 24 小时内的事件。

当我将 xAxis_hrs 向量包装在 strptime(xAxis_hrs, format = "%H") 中时,代码崩溃 - 也就是说 x 轴没有打印出来,我看到,轴错误(1,at = xAxis_hrs, las = 2) : (list) 对象不能被强制输入 'double' 。有什么帮助吗?

【问题讨论】:

  • “崩溃”是什么意思?无论如何,这只是一个错字吗?您的变量名为 xAxis_hrs,而不是 xAxis_hours
  • 问题已修正,谢谢。

标签: r


【解决方案1】:

问题是您将标签传递给错误的命名参数,即at(应该是标签的数字位置)。请改用以下内容:

axis(1, at = xAxis_hrs, labels = strptime(xAxis_hrs, format = "%H"), las = 2)

不幸的是,这并没有改变轴标签不适合绘图并与轴标题冲突的事实。前者可以通过调整绘图边距来修复。我不知道后者有什么好的解决方案,尽管更改时间格式可能会有所帮助:打印完整的分钟和秒(始终为 0)可能没有必要/没有帮助。实际上,您的意思是使用format 而不是strptime

除此之外,从长远来看,我基本上同意另一个推荐 ggplot2 的答案。它使这种事情的痛苦减轻了很多。

【讨论】:

  • 谢谢,我最终使用了 axis(1, at = xAxis_hrs, labels = strftime(xAxis_hrs, format = "%H:%M"), las = 2) 看起来不错。
【解决方案2】:

如果您愿意接受ggplot 解决方案:

library(tidyverse)
library(lubridate)

daily_ggplot <- function(df, selected_day) {
  df_day <- filter(df, day(time_log) == selected_day)
  df_urine <- filter(df_day, event == "u")
  df_drink <- filter(df_day, event != "u")
  TotVol <- sum(df_day$Volume, na.rm = TRUE)
  Date <- floor_date(df_day$time_log[1], 'days')
  DayOfWeek <- weekdays(Date, abbreviate = F)

  plot_title <- paste0("Total drank = ", TotVol, "L on ", DayOfWeek, " Week 1, Apr 2018")

  ggplot(df_drink) +
    aes(time_log, Volume, color = event) +
    geom_point(size = 2) +
    geom_vline(data = df_urine, aes(xintercept = time_log), color = "red", linetype = 3) +
    labs(x = "Hours of Study", ylab = "Volume of Liquid Drank (L)",
         title = plot_title, subtitle = "lines = urination") +
    theme_bw() +
    scale_x_datetime(date_labels = "%H:%M", limits = c(Date, Date + days(1)))
}

daily_ggplot(patient_data, 4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多