Search code examples
rplotlyhovertooltip

How to only have one value in hover text for plot_ly map


I am trying to figure out how to remove all the extra information in my pop-up for my plot_ly map in R so only the country name (mapname) shows on the popup. I do not want the ISO code or the number to show. (https://i.sstatic.net/w7zdK.png). Here is my code

I have tried to change the "name" and "text" arguments within add_trace() but have not had success. Here is my code:


#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


Solution

  • You can adjust that with hovertemplate. Try this

    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)
    )