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

使用latticeExtra的levelplot绘制带2D平滑热力图问题求助

问题:Lattice levelplot结合2D平滑层的可视化问题

背景与数据特征

我尝试用lattice包的levelplot重现带2D平滑层的热力图,数据是模拟得到的x、y条件下的z值,三者特征:

  • x:连续变量,范围120-300
  • y:连续变量,范围0.2-1.8
  • z:连续变量,范围-0.0001-3000

核心需求是突出z<1与z>=1的差异,断点会根据不同实验的z最大值调整(本次z最高超3000,其他实验最高仅30)。

尝试的代码与问题

初始组合图层代码

我最初尝试把散点层和平滑层结合,但平滑层效果不符合预期:

levelplot(z ~ x * y, 
          data, 
          panel = panel.levelplot.points, 
          cex = 0.7,
          col.regions = rocket(25, alpha = .8, direction = -1),
          colorkey = list(at = (breaks = c(1, 20, 50, 150, 500, Inf))),
          scales = list(x=list(at = c(120, 140, 160, 180, 200, 220, 240, 260, 280, 299))),
          xlab = "x", 
          ylab = "y"
) + 
  layer_(panel.2dsmoother(..., n = 100))

单独绘制的散点图与平滑图

我分别绘制了散点图和平滑图:

散点图代码(效果符合预期)

## Scatter
levelplot(z ~ x * y, 
          data,
          panel = panel.levelplot.points, 
          cex = 0.7,
          at = c(-Inf, 1, 30, 100, 500, Inf),   
          col.regions = rocket(25, alpha = .8, direction = -1),
          colorkey = list(
            labels = c("", "1", "30", "100", "500", "")
          ),
          scales = list(x=list(at = c(120, 140, 160, 180, 200, 220, 240, 260, 280, 299))),
          xlab = "x", 
          ylab = "y" ) 

2D平滑图代码

使用cuts=5时效果尚可,但无法确定函数采用的范围,色键标注无法对应需求;传入at参数后,图像完全异常:

## 2dSmooth
levelplot(z ~ x * y, 
          data,
          panel = panel.2dsmoother,
          n = 200, 
          cuts = 5,
          col.regions = rocket(25, alpha = .8, direction = -1),
          colorkey = T,
          scales = list(x=list(at = c(120, 140, 160, 180, 200, 220, 240, 260, 280, 299))),
          xlab = "x", 
          ylab = "y"
) 

提问

  1. 我的操作哪里出错了?尤其是at参数在平滑图中失效的问题,以及组合图层时平滑层不符合预期的原因。
  2. 有没有其他R包可以实现类似levelplot+panel.2dsmoother的2D平滑热力图效果?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 00:55:19