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

在R基础绘图中实现按昼夜切换图表背景颜色

R基础绘图:按昼夜切换背景颜色的实现方法

基于你提供的双轴时间序列绘图代码,以下是通过polygon实现按PARuw>20阈值区分昼夜、切换背景颜色的正确方案:

完整修改代码

# 设置绘图参数
par(mfrow=c(1, 1), mar=c(2, 3, 1, 3))

# 标记昼夜状态:PARuw>20判定为白天
Spring$is_day <- Spring$PARuw > 20

# 定义背景色块的Y轴范围(覆盖左右双轴的全部高度)
y_min <- 0
y_max <- max(6, 1200) # 匹配左侧chla的ylim和右侧PARuw的最大值

# 找出昼夜状态切换的位置,生成连续区间
change_idx <- c(1, which(diff(Spring$is_day) != 0) + 1, nrow(Spring))

# 绘制背景色块(必须先于数据绘制,避免覆盖曲线和点)
for(i in seq(1, length(change_idx)-1)){
  start_idx <- change_idx[i]
  end_idx <- change_idx[i+1]
  # 获取区间起止时间
  x_start <- Spring$date_time[start_idx]
  x_end <- Spring$date_time[end_idx]
  # 匹配对应颜色:白天浅黄,夜晚浅灰
  fill_color <- if(Spring$is_day[start_idx]) "#FFF9E6" else "#F0F0F0"
  # 绘制矩形背景
  polygon(
    x = c(x_start, x_end, x_end, x_start),
    y = c(y_min, y_min, y_max, y_max),
    col = fill_color, border = NA
  )
}

# 绘制左侧Y轴的叶绿素(chla)数据
plot(Spring$date_time, Spring$chla, xlab="", ylab="", type="n", 
     ylim=c(0, 6), las=1)
points(Spring$date_time, Spring$chla, pch=21, col="forestgreen", 
       bg="forestgreen", cex=.8)
lines(Spring$date_time, Spring$chla, col="forestgreen", lwd=5)

# 添加右侧Y轴的太阳辐射(PARuw)数据
par(new=TRUE)
plot(Spring$date_time, Spring$PARuw, xlab="", ylab="", xaxt="n", 
     type="n", yaxt="n", ylim=c(0,1200)) # 显式设置PARuw的Y轴范围
points(Spring$date_time, Spring$PARuw, pch=21, col="yellow2", 
       bg="yellow2", cex=.8)
lines(Spring$date_time, Spring$PARuw, col="yellow2", lwd=5)
axis(side=4, at=seq(0, 1200, by=200), labels=seq(0, 1200, by=200), las=1)

关键说明

  • 绘制顺序:背景色块必须在所有数据绘图前完成,否则会覆盖曲线和点
  • 区间处理:通过diff(Spring$is_day)识别昼夜状态的切换点,将连续的昼夜时间段转换成独立区间
  • Y轴范围:背景色块的Y范围要覆盖左右双轴的全部高度,确保填满整个绘图区域
  • 颜色自定义:示例中白天用浅黄#FFF9E6、夜晚用浅灰#F0F0F0,可根据需求自行调整

原始参考代码与数据

原始绘图代码

par(mfrow=c(1, 1), mar=c(2, 3, 1, 3))

plot(Spring$date_time, Spring$chla, xlab="", ylab="", type="n", 
     ylim=c(0, 6), las=1)
points(Spring$date_time, Spring$chla, pch=21, col="forestgreen", 
       bg="forestgreen", cex=.8)

lines(Spring$date_time, Spring$chla, col="forestgreen", lwd=5)

par(new=TRUE)

plot(Spring$date_time, Spring$PARuw, xlab="", ylab="", xaxt="n", 
     type="n", yaxt="n")

points(Spring$date_time, Spring$PARuw, pch=21, col="yellow2", 
       bg="yellow2", cex=.8)

lines(Spring$date_time, Spring$PARuw, col="yellow2", lwd=5)
axis(side=4, at=seq(0, 1200, by=200), labels=seq(0, 1200, by=200), las=1)

数据框首尾示例

date_time             PARuw  chla
1 2023-04-23 02:00:00 1.954 4.735
2 2023-04-23 02:05:00 1.971 4.769
3 2023-04-23 02:10:00 1.736 4.671
4 2023-04-23 02:15:00 1.586 4.616
5 2023-04-23 02:20:00 1.566 4.912
6 2023-04-23 02:25:00 1.686 4.615 

date_time              PARuw  chla
283 2023-04-24 01:30:00 2.439 5.511
284 2023-04-24 01:35:00 2.250 5.402
285 2023-04-24 01:40:00 2.443 5.381
286 2023-04-24 01:45:00 2.330 5.386
287 2023-04-24 01:50:00 2.220 5.310
288 2023-04-24 01:55:00 2.472 5.313

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 00:38:17