【问题标题】:R: Add breaks in x-axis (date) in ggplot timeseries plotR:在ggplot时间序列图中的x轴(日期)中添加中断
【发布时间】:2018-02-21 16:19:18
【问题描述】:

对于每一天(两周:2015-01-01 - 2015-01-15),我有 24 个(每小时值)要使用 R ggplot2 包进行绘制。日期列dateChr (character 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 等等。有人可以建议我如何添加这样的休息时间吗?

【问题讨论】:

  • ?ggplot2::scale_x_date()
  • 把你的 dateChr 列变成日期。 lubridate 包会很好地做到这一点
  • @RichardTelford:我认为我没有使用过 lubridate。愿意解释一下吗?
  • dmy_hm等函数可以将文本转换为日期

标签: r date ggplot2 axis-labels


【解决方案1】:

通过使用字符类型变量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 ...

【讨论】:

    【解决方案2】:

    旧帖子,但以防其他人在寻找。如 cmets 中所述,最好将日期变量转换为真正的日期字段,然后使用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")
    

    【讨论】:

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