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

无法叠加spatstat超帧中的密度图与对应等高线问题求助

问题描述

我是一名细胞生物学家,在处理重复点数据时将ppp对象存入listof中,能够分别绘制密度图(density.splitppp)和等高线(contour.listof),但无法用add=TRUE将二者叠加。

首次尝试代码:

combined_condition1 <- listof(res_condition1[[1]], res_condition1[[2]], res_condition1[[3]], res_condition1[[4]], res_condition1[[5]], res_condition1[[6]], res_condition1[[7]], res_condition1[[8]])

my_colourmap <- colourmap(plasma(256), range = c(0,0.004))
dens <- density.splitppp(combined_condition1, bw.diggle, positive=TRUE)
plot(dens, col = my_colourmap)
contour(dens, add = T)

报错信息:

Error in extraplot(i, xi, ..., add = !is.null(panel.begin), main = main.panel[i],  : 
  formal argument "add" matched by multiple actual arguments

因为add参数不属于density.splitppp和contour.listof,参考spatstat文档改用hyperframe尝试:

combined_condition1 <- listof(res_condition1[[1]], res_condition1[[2]], res_condition1[[3]], res_condition1[[4]], res_condition1[[5]], res_condition1[[6]], res_condition1[[7]], res_condition1[[8]])
#I know, it's not elegant, but I am a beginner and I hate navigating in lists ;)

my_colourmap <- colourmap(plasma(256), range = c(0,0.004))
dens <- density.splitppp(combined_condition1, bw.diggle, positive=TRUE)

hyp <- hyperframe(Dicht=dens)

plot(hyp)
#... DOES WORK, BUT ONLY DENSITY

contour(hyp$Dicht)
#... DOES WORK, BUT ONLY CONTOUR

plot(hyp, col = my_colourmap, quote(contour(Dicht, add = TRUE)))
#... EMPTY GRAPH WINDOW THAT JUST SAYS "hyp"

已更新所有软件版本,仍无法解决,求可行方案。


解决方案

问题根源是listof对象的绘图函数会批量处理每个子面板,直接调用contour(dens, add=TRUE)会导致内部循环重复传递add参数,触发冲突。以下两种方法可实现密度图与等高线的叠加:

方法1:自定义面板绘图逻辑

利用plot函数的panel参数,指定每个面板内先绘制密度图,再叠加对应元素的等高线:

# 创建密度图对象
combined_condition1 <- listof(res_condition1[[1]], res_condition1[[2]], res_condition1[[3]], res_condition1[[4]], res_condition1[[5]], res_condition1[[6]], res_condition1[[7]], res_condition1[[8]])
my_colourmap <- colourmap(plasma(256), range = c(0,0.004))
dens <- density.splitppp(combined_condition1, bw.diggle, positive=TRUE)

# 定义面板函数:先画密度图,再叠加等高线
custom_panel <- function(x, ...) {
  plot(x, col = my_colourmap, ...)
  contour(x, add = TRUE)
}

# 调用plot并传入自定义面板函数
plot(dens, panel = custom_panel)

方法2:手动循环逐个绘制子图

通过lapply遍历dens中的每个密度图层,手动设置绘图布局,逐个完成密度图与等高线的叠加:

# 设置8个子图的布局(示例为2行4列)
par(mfrow = c(2,4))

# 循环绘制每个子图
lapply(dens, function(x) {
  plot(x, col = my_colourmap)
  contour(x, add = TRUE)
})

# 恢复默认绘图布局
par(mfrow = c(1,1))

两种方法均能避免add参数冲突的问题,实现每个子面板中密度图与等高线的叠加。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 09:12:28