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

R语言使用plotly绘制等高线图时如何添加并高亮指定单点

R语言Plotly等高线图添加高亮单点方案

要在你现有等高线图的基础上添加x=0.3、y=4的高亮坐标点,只需新增散点图层即可,完整可运行代码如下:

# H0计算函数
h=function(pl,u,g){
sigma=sqrt(2)*qnorm((1+g)/2)
x=pl/u
h=pnorm(((log(x)/sigma) + (sigma/2)))
return(h)
}

# 填充计算矩阵
pl=1.5
u=seq(1.5,9,0.1)
g=seq(.2,.7,0.1)
M=matrix(0,nrow=length(u),ncol=length(g))

for(i in 1:length(u)) {
for(j in 1:length(g)) {
M[i,j]=h(pl,u[i],g[j])
}
}

rownames(M)=u
colnames(M)=g

# 绘图逻辑
library(plotly)
fig <- plot_ly(
  type = 'contour',
  y = u/pl,  
  x = g,
  z = M,
  colorscale = 'Jet',
  autocontour = F,
  contours = list(
    start = 0.2,
    end = 0.8,
    size = 0.1,
    showlabels=TRUE
    )
  )%>%
  layout(title = '贫困发生率指数', xaxis = list(title = '基尼系数'), 
         yaxis = list(title = '均值/贫困线 (u/z)')) %>%
  # 新增高亮单点的散点图层
  add_trace(
    x = 0.3,
    y = 4,
    type = 'scatter',
    mode = 'markers',
    inherit = FALSE,
    marker = list(
      color = 'red',
      size = 12,
      line = list(
        color = 'white',
        width = 2
      )
    ),
    name = '目标坐标点'
  )

fig

效果调整说明

  • inherit = FALSE 用于避免继承等高线图层的z轴等参数,防止图层渲染报错
  • 你可以按需修改marker下的参数调整高亮效果:修改size调整点的大小,修改color更换点的填充色,调整line参数可以更换外边框的颜色和粗细

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 11:06:03