gpt4 book ai didi

r - 在 ggplot2 中绘制白天(无日期)

转载 作者:行者123 更新时间:2023-12-04 10:49:18 25 4
gpt4 key购买 nike

我想在 ggplot2 中散点图一个数字向量与白天(%H:%M)的关系。

我明白那个

as.POSIXct(dat$daytime, format = "%H:%M")

是格式化时间数据的方法,但输出向量仍将包含日期(今天的日期)。因此,轴刻度将包括日期(3 月 22 日)。
ggplot(dat, aes(x=as.POSIXct(dat$daytime, format = "%H:%M"), y=y, color=sex)) +
geom_point(shape=15,
position=position_jitter(width=0.5,height=0.5))

image of the plot output

有没有办法完全摆脱日期,尤其是在情节轴上?
(我在留言板上找到的所有信息似乎都是指旧版本的 ggplot,现在已经不存在 date_format 参数)

最佳答案

您可以向 labels 提供功能scale_x_datetime() 的参数或使用 date_label范围:

# create dummy data as OP hasn't provided a reproducible example
dat <- data.frame(daytime = as.POSIXct(sprintf("%02i:%02i", 1:23, 2 * (1:23)), format = "%H:%M"),
y = 1:23)
# plot
library(ggplot2)
ggplot(dat, aes(daytime, y)) + geom_point() +
scale_x_datetime(labels = function(x) format(x, format = "%H:%M"))

编辑 : 或者,更简洁的你可以使用 date_label参数(感谢 aosmith 用于 suggestion )。
ggplot(dat, aes(daytime, y)) + geom_point() + 
scale_x_datetime(date_label = "%H:%M")

enter image description here

关于r - 在 ggplot2 中绘制白天(无日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42950909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com