使用lakeanalyzer绘制wtr.heat.map报错:非数值参数参与二元运算
问题:使用lakeanalyzer包绘制水温热图时触发数值运算错误
报错信息
Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : non-numeric argument to binary operator
已尝试的操作及代码
加载数据集
getwd() setwd("/Users/heathershaw/Desktop/DepthProfiles2023/TalusSouthHeatMaps") TalusSouth <- read.csv( file = "HeatMapTS.csv") View(TalusSouth) str(TalusSouth) datetime..GMT. <- as.POSIXct(TalusSouth$datetime..GMT., format = "%Y-%m-%d %H:%M:%S", Tz="GMT") datetime..GMT.
转换为数据框
TalusSouthdf<-as.data.frame(TalusSouth)
绘制热图
plot2<-wtr.heat.map (TalusSouthdf, plot.axes = {axis(1, at = as.POSIXct(seq.Date(from = as.Date("2023-07- 06"), to = as.Date("2023-10-05"), by = "1 week")), labels = format(seq.Date(from = as.Date("2023-78-06"), to = as.Date("2023-10-05"), by = "1 week"), "%b %d")) axis(2)} )
问题排查与解决方案
核心问题分析
- 日期列未正确替换:仅单独创建了POSIXct类型的
datetime..GMT.变量,但未将其替换回原数据框,导致数据框中的日期列仍为字符型,wtr.heat.map在内部运算时因字符类型参与数值运算触发报错。 - 无效日期值:绘图代码中
seq.Date的起始日期写成2023-78-06,月份78为无效值,会导致日期生成失败。 - 数据格式要求:
wtr.heat.map要求输入数据框的第一列为POSIXct类型的日期时间,后续列必须是数值型的水温数据,列名需对应深度值(如"0.5", "2"等)。
修正后的代码
1. 正确转换并替换日期列
setwd("/Users/heathershaw/Desktop/DepthProfiles2023/TalusSouthHeatMaps") TalusSouth <- read.csv("HeatMapTS.csv") # 将转换后的日期时间替换回数据框对应列 TalusSouth$datetime..GMT. <- as.POSIXct(TalusSouth$datetime..GMT., format = "%Y-%m-%d %H:%M:%S", tz = "GMT") # 检查数据格式,确认datetime..GMT.为POSIXct类型 str(TalusSouth)
2. 修正绘图代码中的日期错误
# 绘制热图 plot2 <- wtr.heat.map(TalusSouth, plot.axes = { axis(1, at = as.POSIXct(seq.Date(from = as.Date("2023-07-06"), to = as.Date("2023-10-05"), by = "1 week")), labels = format(seq.Date(from = as.Date("2023-07-06"), to = as.Date("2023-10-05"), by = "1 week"), "%b %d")) axis(2) })
额外检查项
- 确认数据框中除日期列外,其余列均为数值型(可通过
str(TalusSouth)查看),若有非数值列需提前清理。 - 确保深度列的列名为数值格式(如"0.5"而非"Depth_0.5"),否则
wtr.heat.map无法识别深度层级。
内容的提问来源于stack exchange,提问作者Heather Shaw
相关产品推荐
相关产品推荐

