繁体   English   中英

R:在 ggplot 时间序列图中的 x 轴(日期)中添加中断

[英]R: Add breaks in x-axis (date) in ggplot timeseries plot

对于每一天(两周: 2015-01-01 - 2015-01-15 ),我有 24 个(每小时值)使用 R ggplot2 包进行绘图。 日期列dateChrcharacter format )如下所示:

> str(Data$dateChr)
chr [1:360] "1/1/2015 2:00" "1/1/2015 3:00" "1/1/2015 4:00" "1/1/2015 5:00" 
"1/1/2015 6:00" "1/1/2015 7:00" ...

这是代码,我正在使用:

ggplot() + 
geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
theme_bw() +
xlab("Date") + 
ylab("Value")

情节是这样的:

阴谋

x-axis看起来很糟糕。 我想在x-axis添加中断,以便它只显示 4 天中断的日期(没有小时或时间戳),即 2015-01-01、2015-01-04、2015-01-08 等等。 有人可以建议我如何添加这样的休息时间吗?

通过使用字符类型变量dateChr ,OP选择了离散的 X轴。

scale_x_discrete()函数可用于自定义离散轴的外观。 根据help("discrete_scale") ,它采用一个break参数来控制中断(和标签)。 可能要break一种输入类型是

一个函数,当使用单个参数调用该函数时,它给出了比例尺的极限,它返回一个字符矢量,该字符矢量指定要显示的中断。

因此,额外调用scale_x_discrete()

library(ggplot2)
ggplot() + 
  geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
  theme_bw() +
  xlab("Date") + 
  ylab("Value") + 
  scale_x_discrete(breaks = function(x) x[seq(1, length(x), by = 4*24)])

我们得到

在此处输入图片说明

每隔4天显示一次休息和标签。


现在,OP已要求仅显示日期(无小时或时间戳) 这需要操纵dateChr但仅用于绘制标签:

# define named functions for breaks
my_breaks <- function(x) x[seq(1, length(x), by = 4*24)]
library(ggplot2)
ggplot() + 
  geom_line(data = Data, aes(x = dateChr, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateChr, y = val2, group=1), color = "blue") +
  theme_bw() +
  xlab("Date") + 
  ylab("Value") + 
  scale_x_discrete(breaks = my_breaks,
                   labels = my_breaks(stringr::str_sub(Data$dateChr, 1, 10)))

在此处输入图片说明

数据

不幸的是,OP没有提供数据来重现图形。 因此,我必须组成自己的样本数据集来模拟OP的数据。

df1 <- data.table::fread("https://tidesandcurrents.noaa.gov/api/datagetter?product=wind&application=NOS.COOPS.TAC.MET&begin_date=20150101&end_date=20150114&station=8594900&time_zone=GMT&units=metric&interval=h&format=csv")
df2 <- data.table::fread("https://tidesandcurrents.noaa.gov/api/datagetter?product=wind&application=NOS.COOPS.TAC.MET&begin_date=20150101&end_date=20150114&station=8638999&time_zone=GMT&units=metric&interval=h&format=csv")

Data <- data.frame(dateChr = format(as.POSIXct(df1$`Date Time`), "%d/%m%/%Y %H:%M"),
                   val1 = df1$Speed, val2 = df2$Speed, stringsAsFactors = FALSE)
str(Data)
 'data.frame': 336 obs. of 3 variables: $ dateChr: chr "01/01/2015 00:00" "01/01/2015 01:00" "01/01/2015 02:00" "01/01/2015 03:00" ... $ val1 : num 1.42 0.51 0.91 2.08 1.3 1.27 2.08 2.33 1.7 1.95 ... $ val2 : num 1.1 0.1 2.7 3.5 4 4.1 4.1 4 3.8 4.4 ... 

旧帖子,但以防万一其他人正在寻找。 如评论中所述,最好将日期变量转换为真正的日期字段,然后使用scale_x_date

library(ggplot2)
Data$dateDate <- lubridate::dmy_hm(Data$dateChr)

ggplot() + 
  geom_line(data = Data, aes(x = dateDate, y = val1, group=1), color = "red") +
  geom_line(data = Data, aes(x = dateDate, y = val2, group=1), color = "blue") +
  theme_bw() +
  scale_x_date("Date", date_breaks = "4 days") + 
  ylab("Value")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM