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

如何将手动水位测量点叠加至自动监测折线图?

实现手动测量点与自动监测折线图叠加

当然可以实现将手动测量点叠加到自动监测折线图中,以下是修正并优化后的代码方案:

步骤说明

  • 统一加载所需工具包,避免重复加载
  • 修正自动监测数据的日期处理逻辑
  • 在自动监测折线图基础上,直接叠加手动测量散点

完整代码

# 加载所需包(整合readxl、dplyr、ggplot2等工具)
library(tidyverse)

# 导入自动监测数据
MW3 <- read_excel("C:/Users/923721/OneDrive - Royal HaskoningDHV/Internship documents/Data_Weustenrade/Waterstanden/MW3.xlsx")

# 计算日均水位:将时间戳按天截断后分组求均值
Water_level_perday <- MW3 %>%
  mutate(date = floor_date(Date, unit = "day")) %>%
  group_by(date) %>%
  summarize(mean_waterlevel = mean(WaterLevel_NAP_m, na.rm = TRUE))  # 忽略缺失值避免报错

# 导入手动测量数据
Manual_waterlevel_3 <- read_excel("C:/Users/923721/OneDrive - Royal HaskoningDHV/Internship documents/Data_Weustenrade/Waterstanden/Manual_waterlevel_3.xlsx")

# 绘制叠加图:先画自动监测折线,再叠加手动散点
Combined_plot <- ggplot() +
  # 自动监测日均水位折线
  geom_line(data = Water_level_perday, aes(x = date, y = mean_waterlevel), color = "blue", linewidth = 0.8) +
  # 手动测量点:指定单独数据源,用红色实心点突出显示
  geom_point(data = Manual_waterlevel_3, aes(x = Datum, y = H20_NAP), color = "red", size = 3, shape = 19) +
  # 设置标签与主题
  labs(x = "时间", y = "水位_NAP (m)", title = "自动监测日均水位与手动测量对比",
       caption = "蓝色折线:自动监测日均水位 | 红色点:手动测量水位") +
  theme_classic()

# 输出图形
print(Combined_plot)

关键细节说明

  • 手动指定geom_point()的data参数:由于折线图和散点图使用不同数据源,必须在geom_point()中明确指定手动测量数据集
  • 样式区分:通过颜色、大小、形状手动区分两类数据,提升可视化对比效果
  • 日期对齐:两类数据的日期列均为POSIXct格式,ggplot会自动对齐x轴,无需额外转换

内容的提问来源于stack exchange,提问作者Stefanie van den Berg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:35:32