You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中绘制时间序列时如何设置X轴每月一个刻度?

问题描述

我用以下R代码绘制时间序列,想要X轴每月显示一个刻度,但现在生成的刻度太多,该怎么解决?

代码

library(readxl)
dataset <- read_excel(choose.files())
dataset <- data.frame(dataset)
dataset$Inicio <- as.Date(dataset$Inicio, format=c("%d/%m/%Y"))

plot(Temp.media.de.las.medias ~ Inicio, dataset, col="black", ylim = c(0, 40), type = "l", lwd=2, xaxt='n')
axis(1, dataset$Inicio, format(dataset$Inicio, "%b %m"), cex.axis = .5)

grid(nx = NULL, ny = NULL,
     lty = 1,      # Grid line type
     col = "gray", # Grid line color
     lwd = 1)

数据示例

dput(head(dataset$Temp.media.de.las.medias, 30))
# 输出:
c(25.36, 25.39, 23.6, 22.53, 23.59, 20.7, 18.99, 18.37, 17.46, 
16.13, 11.82, 14.52, 15.33, 13.39, 13.36, 13.41, 12.96, 10.85, 
8.68, 7.72, 6.04, 8.96, 12.35, 13.52, 11.41, 9.31, 9.72, 12.29, 
14.95, 14.64)

dput(head(dataset$Inicio, 30))
# 输出:
structure(c(17763, 17770, 17777, 17784, 17791, 17798, 17805, 
17812, 17819, 17826, 17833, 17840, 17847, 17854, 17861, 17868, 
17875, 17882, 17889, 17897, 17903, 17910, 17917, 17924, 17931, 
17938, 17945, 17952, 17959, 17966), class = "Date")

class(dataset$Temp.media.de.las.medias)
# [1] "numeric"

class(dataset$Inicio)
# [1] "Date"

当前效果

绘图效果


解决方案

问题出在你直接用dataset$Inicio作为刻度位置,而数据是每周一条,因此生成了大量刻度。要实现每月一个刻度,需要生成每月的代表日期作为刻度位置,以下是几种可行方法:

方法1:基础R seq.Date生成月度刻度

先确定时间范围起止,生成每月第一天的序列作为刻度位置:

library(readxl)
dataset <- read_excel(choose.files())
dataset <- data.frame(dataset)
dataset$Inicio <- as.Date(dataset$Inicio, format=c("%d/%m/%Y"))

# 绘制主图,隐藏默认X轴
plot(Temp.media.de.las.medias ~ Inicio, dataset, col="black", ylim = c(0, 40), type = "l", lwd=2, xaxt='n')

# 生成每月第一天的刻度序列
date_range <- range(dataset$Inicio)
monthly_ticks <- seq.Date(
  from = as.Date(paste0(format(date_range[1], "%Y-%m"), "-01")),
  to = date_range[2],
  by = "month"
)

# 添加X轴刻度
axis(1, at = monthly_ticks, labels = format(monthly_ticks, "%b %Y"), cex.axis = .7)

# 添加网格
grid(nx = NULL, ny = NULL, lty = 1, col = "gray", lwd = 1)
  • 用%b %Y显示月份缩写+年份,避免跨年度同月份混淆;若仅需月份,保留%b %m即可。

方法2:用axis.Date自动生成月度刻度

axis.Date是专门处理日期轴的函数,可直接指定刻度间隔:

# 主图部分不变
plot(Temp.media.de.las.medias ~ Inicio, dataset, col="black", ylim = c(0, 40), type = "l", lwd=2, xaxt='n')

# 生成月度刻度
axis.Date(1, at = seq(min(dataset$Inicio), max(dataset$Inicio), by = "month"), 
          format = "%b %Y", cex.axis = .7)

grid(nx = NULL, ny = NULL, lty = 1, col = "gray", lwd = 1)
  • by = "month"直接指定刻度间隔为一个月,代码更简洁。

方法3:lubridate包简化日期处理

若已安装lubridate包,可更便捷地提取月份信息,确保每个月仅显示一个刻度:

library(lubridate)
library(readxl)

dataset <- read_excel(choose.files())
dataset <- data.frame(dataset)
dataset$Inicio <- dmy(dataset$Inicio) # 自动识别日/月/年格式

plot(Temp.media.de.las.medias ~ Inicio, dataset, col="black", ylim = c(0, 40), type = "l", lwd=2, xaxt='n')

# 提取每个月的第一个日期作为刻度
dataset$year_month <- floor_date(dataset$Inicio, "month")
unique_months <- unique(dataset$year_month)

axis(1, at = unique_months, labels = format(unique_months, "%b %Y"), cex.axis = .7)

grid(nx = NULL, ny = NULL, lty = 1, col = "gray", lwd = 1)
  • floor_date将日期向下取整到当月第一天,unique去重后得到每个月的唯一刻度位置。

内容的提问来源于stack exchange,提问作者sermomon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 04:39:25