如何在plot_ly地图的悬停文本中只有一个值

问题描述 投票:0回答:1

我正在尝试找出如何删除 R 中的plot_ly 地图弹出窗口中的所有额外信息,以便弹出窗口中仅显示国家/地区名称(地图名称)。我不想显示 ISO 代码或编号。 (https://i.stack.imgur.com/w7zdK.png)。 这是我的代码

我尝试更改 add_trace() 中的“名称”和“文本”参数,但没有成功。这是我的代码:


#plotly map
library(plotly)

countrylabeldata<-iso3166

countrylabeldata <- rename(countrylabeldata, "iso-a3" = a3)

Swahili_countries <- c("BDI","OMN", "RWA","SSD", "SOM", "MDG", "ZMB")
Official_Swahili_countries<-c("TZA", "UGA", "KEN", "COD")

countrylabeldata$include <- ifelse(countrylabeldata$`iso-a3` %in% Swahili_countries, 1, 0)

countrylabeldata$include <- ifelse(countrylabeldata$`iso-a3` %in% Official_Swahili_countries, 2, countrylabeldata$include)


ncols<-3

zseq <- seq(0,1,length.out=ncols+1)

cols <- c("lightgrey", "red","purple")

colorScale <- data.frame(
     z = c(0,rep(zseq[-c(1,length(zseq))],each=2),1),
     col=rep(cols,each=2)
)   

colorScale$col <- as.character(colorScale$col)



g <- list(
  showframe = FALSE,
  showcoastlines = FALSE,
  projection = list(type = 'orthographic')
)
l <- list(color = toRGB("grey"), width = 0.5)
fig <- plot_geo(countrylabeldata)


fig <- fig %>% add_trace(
    z = ~include, color = ~include, colorscale=colorScale,
    text = ~mapname, locations = ~`iso-a3`,
    colorbar=list(tickmode='array', title="Level of Swahili Spoken", tickvals=c(.3,1.0,1.7), ticktext=c("Not Widely Spoken","Spoken","Official Language"),
                  ticklen=.1)
  )



fig <- fig %>% layout(
    title = 'Swahili Speaking Countries',
    geo = g,
    margin = list(l = 20, r = 20, b = 20, t = 50),
         annotations = list(x = 1.4, y = .2, text = 'Source:<a href="https://www.cia.gov/the-world-factbook/field/languages/">CIA World Factbook</a>',
                            xref='paper', yref='paper', showarrow = F, 
                            xanchor='right', yanchor='auto', xshift=0, yshift=0,
                            font = list(size = 10)))

fig <- fig %>%colorbar(title = "Swahili", x = 1, y = 0.75)


fig

r plotly hover tooltip
1个回答
0
投票

您可以使用

hovertemplate
进行调整。试试这个

fig <- plot_geo(countrylabeldata)

fig <- fig %>% add_trace(
  z = ~include, color = ~include, colorscale=colorScale,
  #text = ~mapname, 
  hovertemplate = ~paste(mapname,"<extra></extra>"),
  locations = ~`iso-a3`,
  colorbar=list(tickmode='array', title="Level of Swahili Spoken", tickvals=c(.3,1.0,1.7), ticktext=c("Not Widely Spoken","Spoken","Official Language"),
                ticklen=.1)
)
© www.soinside.com 2019 - 2024. All rights reserved.