将X轴从日期转换为ggplot2中的HRS

发布于 2025-02-02 21:03:58 字数 559 浏览 1 评论 0原文

在48小时内,我有一个受体压力探针的记录。从Excel导入数据后,我获得了两个vars时间(class =“ posixct”和probe('numeric')。

'data.frame':   3647 obs. of  2 variables:
$ Date : POSIXct, format: "2020-01-15 17:34:02" "2020-01-15 17:34:42"...
$ probe: num  31.6 35.8 29.9 29.1 30.1...

我使用ggplot2和geom_line绘制vars,我得到了此图 “

x轴显示时间(总48 h) 但是,当我尝试使用以下方式格式化时间时:

library(hms) 
data$time<-as_hms(data$Date)

我有一个凌乱的情节。我尝试了不同的方法将“数据$时间”转换为不同的量表,但我无法管理。 任何帮助将不胜感激

I have a recording during 48 h of a probe of pressure of a recipient. After importing data from Excel I get two vars Time (class= "POSIXct" and Probe ('numeric').

'data.frame':   3647 obs. of  2 variables:
$ Date : POSIXct, format: "2020-01-15 17:34:02" "2020-01-15 17:34:42"...
$ probe: num  31.6 35.8 29.9 29.1 30.1...

I plot both vars using ggplot2 and geom_line and I get this graph
Figure

The X axis shows time (total 48 h)
However, when I try to format time by using:

library(hms) 
data$time<-as_hms(data$Date)

I got a messy plot. I have tried different methods to convert 'data$time' to a different scales but I cannot manage this.
Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2025-02-09 21:03:58

通过scale_x_datetime来解决这一问题,通过创建breaks向量并使用date_labels格式字符串标记。

我创建了一个测试数据集,因为问题中没有。

set.seed(2022)     # make the example reproducible

start <- as.POSIXct("2020-01-15 17:34:02")
end <- start + 48*60*60
Date <- seq(start, end, by = "15 mins")
probe  <- cumsum(rnorm(length(Date)))
data <- data.frame(Date, probe)

library(ggplot2)

breaks_start <- as.POSIXct(format(start, "%Y-%m-%d"))
breaks_end <- as.POSIXct(format(end + 24*60*60, "%Y-%m-%d"))

ggplot(data, aes(Date, probe)) +
  geom_line(color = "darkred", size = 1.2) +
  scale_x_datetime(
    breaks = seq(breaks_start, breaks_end, by = "12 hours"),
    date_labels = "%H:%M"
  )

“”

在2022-05-28上由 reprex软件包(v2.0.1)

This is solved with scale_x_datetime by creating a breaks vector and labeling with a date_labels format string.

I have created a test data set, since there is none in the question.

set.seed(2022)     # make the example reproducible

start <- as.POSIXct("2020-01-15 17:34:02")
end <- start + 48*60*60
Date <- seq(start, end, by = "15 mins")
probe  <- cumsum(rnorm(length(Date)))
data <- data.frame(Date, probe)

library(ggplot2)

breaks_start <- as.POSIXct(format(start, "%Y-%m-%d"))
breaks_end <- as.POSIXct(format(end + 24*60*60, "%Y-%m-%d"))

ggplot(data, aes(Date, probe)) +
  geom_line(color = "darkred", size = 1.2) +
  scale_x_datetime(
    breaks = seq(breaks_start, breaks_end, by = "12 hours"),
    date_labels = "%H:%M"
  )

Created on 2022-05-28 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文