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

R语言apg包SoilProfileCollection贡献占比绘图颜色异常问题

解决apg包绘制深度图时贡献占比颜色异常的问题

我能看到你遇到的问题:贡献占比的颜色没有按分组(Vegetated/Moderately Degraded)来分配,反而逐深度交替,同时还有the condition has length > 1 and only the first element will be used的警告——这个警告就是问题的根源。

问题原因分析

警告提示cf.col参数的长度大于1,但函数内部的判断逻辑只处理了第一个元素,导致颜色分配逻辑混乱。panel.depth_function的cf.col参数需要的是与数据行一一对应的颜色向量,而不是直接传两个分组的颜色值让函数自动映射。你当前传的cf.col=c('black', 'RoyalBlue')被当成了单个值处理,只有黑色生效,剩下的颜色逻辑出错,才会出现逐深度交替的异常。

解决方案

你需要根据数据的分组(g$which)生成一个和数据行数一致的颜色向量,显式指定每个贡献占比所属分组的颜色:

  1. 把原来的cf.col=c('black', 'RoyalBlue')替换成:

    cf.col = ifelse(g$which == 'veg', 'black', 'RoyalBlue')
    

    这样每个数据点的贡献占比颜色会和它的分组(veg/mod)完全对应,第一个分组(veg)用黑色,第二个(mod)用蓝色。

  2. 确保sync.colors=TRUE保留,这样中位数线和贡献占比的颜色能保持同步。

修改后的完整代码

idx_veg <- which(a$LU == 'Vegetated LU')
veg <- slab(a[idx_veg, ], fm= ~ TOC + TIC + Clay + Silt + Sand + Nitrogen..)
idx_mod <- which(a$LU == 'Moderately Degraded LU')
mod <- slab(a[idx_mod, ], fm= ~ TOC + TIC + Clay + Silt + Sand + Nitrogen..)

# combine with the collection-wide aggregate data
g <- make.groups(veg=veg, mod=mod)

xyplot(top ~ p.q50 | variable, 
       groups=which, 
       data=g, 
       ylab='Depth [cm]', 
       xlab='median bounded by 25th and 75th percentiles', 
       lower=g$p.q25, 
       upper=g$p.q75, 
       ylim=c(60,-2), # ylim defines the y-axis depth
       panel=panel.depth_function, 
       alpha=0.15, 
       sync.colors=TRUE, # alpha = transparency of 25-75% interval
       par.settings=list(superpose.line=list(col=c('black', 'RoyalBlue'), lwd=1, lty=c(1,2))), # median line, colour and width
       prepanel=prepanel.depth_function, 
       cf=g$contributing_fraction, 
       # 修改这里:根据分组生成对应颜色向量
       cf.col = ifelse(g$which == 'veg', 'black', 'RoyalBlue'), 
       cf.interval=10, # contributing fractions (right hand side x-axis)
       layout=c(6,1), 
       strip=strip.custom(bg=grey(0.8)), # Layout = arrangement of panels of plots
       scales=list(x=list(tick.number=4, alternating=3, relation='free')),# x, y-axis
       auto.key=list(columns=2, lines=TRUE, points=FALSE) 
)

额外说明

这样修改后,每个10cm深度的veg分组贡献占比会显示黑色,mod分组显示蓝色,完全符合你的需求,同时那个长度不匹配的警告也会消失——因为现在cf.col的长度和数据行数一致,函数能正确处理每个点的颜色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:41:11