R plotly突出一点

R plotly highlight one point

我正在尝试为已经构建的绘图添加一个点,并在图表上突出显示这一点。 例如,我想添加点 (x=0.3, y=4) 并将其定位在绘图

这是代码

#function of 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)
}

#fill in matrix

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


#plot

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 = 'Heacount Index', xaxis = list(title = 'Gini index'), 
         yaxis = list(title = 'Mean/Poverty Line (u/z)'))


  fig 

您可以为此使用 add_trace

# function of 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)
}

# fill in matrix
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


# plot
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 = 'Heacount Index', xaxis = list(title = 'Gini index'), 
         yaxis = list(title = 'Mean/Poverty Line (u/z)'))


fig <- add_trace(fig, x = 0.3, y = 4, type = "scatter", mode = "markers", color = I("red"), inherit = FALSE, name = "myPoint")
fig