Black Lives Matter. Please consider donating to Black Girls Code today.

Hover text not working

Hi the hover text does not work in this script. Any idea ??
It should display the text withing the text variable below.

mm <- structure(list(Taxa = structure(1:13, .Label = c(“Streptococcus salivarius”,
“Lactobacillus gasseri”, “Escherichia coli”, “Shigella boydii”,
“Pseudomonas aeruginosa”, “Staphylococcus aureus”, “Enterococcus faecalis”,
“Staphylococcus epidermidis”, “Bacteroides vulgatus”, “Bacillus sp.”,
“Clostridium perfringens”, “Propionibacterium acnes”, “Bifidobacterium longum”
), class = “factor”), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(“Relative_abundance_median”,
“Relative_abundance_mean”), class = “factor”), value = c(6.77,
6.28, 13.76, 13.78, 7.25, 6.35, 6.82, 6.13, 6.36, 6.77, 5.34,
7.17, 7.2)), .Names = c(“Taxa”, “variable”, “value”), row.names = c(NA,
13L), class = “data.frame”)

col_vector1=c("#76021F", “#3BF13B”, “#69E4ED”, “#956FF5”, “#FCDA61”, “#075B0E”,
"#F7ABC4", “#324961”, “#F83821”, “#DE1A88”, “#6BFDB2”, “#1887DB”,
"#B36317")

leg <- list(
bgcolor = “#EEE0E5”,
bordercolor = “#FFF0F5”,
borderwidth = 2
)
ang <- list(tickangle = 45)
mar <- list(
l = 50,
r = 50,
b = 120,
t = 50,
pad = 4,
autoexpand=T
)

                     Taxa                  variable value

1 Streptococcus salivarius Relative_abundance_median 6.77
2 Lactobacillus gasseri Relative_abundance_median 6.28
3 Escherichia coli Relative_abundance_median 13.76
4 Shigella boydii Relative_abundance_median 13.78
5 Pseudomonas aeruginosa Relative_abundance_median 7.25
6 Staphylococcus aureus Relative_abundance_median 6.35
7 Enterococcus faecalis Relative_abundance_median 6.82
8 Staphylococcus epidermidis Relative_abundance_median 6.13
9 Bacteroides vulgatus Relative_abundance_median 6.36
10 Bacillus sp. Relative_abundance_median 6.77
11 Clostridium perfringens Relative_abundance_median 5.34
12 Propionibacterium acnes Relative_abundance_median 7.17
13 Bifidobacterium longum Relative_abundance_median 7.20

text = paste(‘Percent abundance:’,mm$value,"
", paste(“Species”,":",as.character(mm$variable),sep=""),sep="")

pl <- plot_ly(mm,
x = ~Taxa,
y = ~value,
color = ~Taxa,
mode=“markers”,
type = ‘bar’ ,
marker = list(color = col_vector1
text = text,
hoverinfo=text ) %>%
layout(margin=mar,
# xaxis=ang,
# bargap=0.7,
# legend=leg,
title=“Realtive Abundance”,
xaxis = list(title =“Percent abundance”),
yaxis = list(title = “Percent abundance”)
)

Hi David, I know this is quite late, but you have misused the text argument here is the working code:

mm <- structure(list(Taxa = structure(1:13, .Label = c("Streptococcus salivarius", 
                                                       "Lactobacillus gasseri", "Escherichia coli", "Shigella boydii", 
                                                       "Pseudomonas aeruginosa", "Staphylococcus aureus", "Enterococcus faecalis", 
                                                       "Staphylococcus epidermidis", "Bacteroides vulgatus", "Bacillus sp.", 
                                                       "Clostridium perfringens", "Propionibacterium acnes", "Bifidobacterium longum"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Relative_abundance_median", 
                                                                                             "Relative_abundance_mean"), class = "factor"), value = c(6.77, 
                                                                                                                                                      6.28, 13.76, 13.78, 7.25, 6.35, 6.82, 6.13, 6.36, 6.77, 5.34, 
                                                                                                                                                      7.17, 7.2)), .Names = c("Taxa", "variable", "value"), row.names = c(NA, 
                                                                                                                                                                                                                          13L), class = "data.frame")

col_vector1=c("#76021F", "#3BF13B", "#69E4ED", "#956FF5", "#FCDA61", "#075B0E", 
              "#F7ABC4", "#324961", "#F83821", "#DE1A88", "#6BFDB2", "#1887DB", 
              "#B36317")

leg <- list(
  bgcolor = "#EEE0E5",
  bordercolor = "#FFF0F5",
  borderwidth = 2
)
ang <- list(tickangle = 45)
mar <- list(
  l = 50,
  r = 50,
  b = 120,
  t = 50,
  pad = 4,
  autoexpand=T
)

text = 

pl <- plot_ly(mm, 
              x = ~Taxa, 
              y = ~value,
              color = ~Taxa,
              mode="markers",
              type = 'bar' ,
              marker = list(color = col_vector1),
                            text = ~paste('Percent abundance:',value,"
", paste("Species",":",as.character(variable),sep=""),sep=""),
                            hoverinfo='text' ) %>%
                layout(margin=mar,
                       title="Realtive Abundance", 
                       xaxis = list(title ="Percent abundance"),
                       yaxis = list(title = "Percent abundance")
                )

I came across this because I have an issue with the hover text with scattermapbox. The key difference between the two is that one has specified hoverinfo = 'text' where as the other one does not specify this argument. When the argument is not specified the hover text doesnt show for all points. Any help?

library(plotly)

Sys.setenv('MAPBOX_TOKEN' = 'mapbox_token' )

Test <- data.frame(
Postcode = c(801, 820, 820, 822, 822),
Name_Organisation = c("Google", "Apple", "Microsoft","IBM","Rstudio"),
Org_Type = c("Training Provider","National Laboratory & Testing Facility","National Laboratory & Testing Facility","National Laboratory & Testing Facility", "Training Provider"),
State = c("NT","NT","NT","NT","NT"),
Latitude = c(-12.80081,-12.41066,-12.40957,-12.79939,-12.89939),
Longitude = c(130.9598,130.8604,130.8598,131.1294,131.1294),
RDOrgCap = c("Training Centres & Facilities","Testing facilities (public & private)","Testing facilities (public & private)","Testing facilities (public & private)","Testing facilities (public & private)")
)

basic <- list(method = "relayout",
              args = list(list(mapbox.style = "basic")),
              label = "Basic")

dark <- list(method = "relayout",
             args = list(list(mapbox.style = "dark")),
             label = "Dark")

satellite <- list(method = "relayout",
                  args = list(list(mapbox.style = "satellite")),
                  label = "Satellite")  

#Hovertext works
Map <- plot_mapbox(data = Test, lon=~Longitude, lat = ~Latitude, type = "scattermapbox", mode = "markers",split = ~RDOrgCap,text = ~paste("State: ",State,"<br>","Region (Postcode): ",Postcode,"<br>", "Organisation Name: ",Name_Organisation, "<br>", "Organisation Type: ",Org_Type,sep = "" )) %>%
  layout(mapbox = list(zoom = 3.5,
                       center = list(lat =-27 ,
                                     lon =133)),  legend = list(opacity= 1)
  ) %>%
  layout(updatemenus = list(list(x = -0.01,
                                 y = 1,buttons= list(basic,dark,satellite))))


Map

#Hovertext does not work
Map_1 <- plot_mapbox(data = Test, lon=~Longitude, lat = ~Latitude, type = "scattermapbox", mode = "markers", hoverinfo = 'text',split = ~RDOrgCap,text = ~paste("State: ",State,"<br>","Region (Postcode): ",Postcode,"<br>", "Organisation Name: ",Name_Organisation, "<br>", "Organisation Type: ",Org_Type,sep = "" )) %>%
  layout(mapbox = list(zoom = 3.5,
                       center = list(lat =-27 ,
                                     lon =133)),  legend = list(opacity= 1)
  ) %>%
  layout(updatemenus = list(list(x = -0.01,
                                 y = 1,buttons= list(basic,dark,satellite))))


Map_1

Hi,
Thanks for the late update. In fact I could manage to fix it. Thanks anyway.
Regarding your info i would try the following since hoverinfo is a nightmare

# Let´s try to hack the code and see if it works. Once you have generated the Map_1 ,type the following:

Map_1 <- plotly_build(Map_1)$x
l <- length(Map_1$data)
for (i in 1:l)
{
Map_1$data[[i]]$hoverinfo <- NULL
}
Map_1 <- as_widget(Map_1)
Map_1

Works well for me.

49

1 Like

Hi David,

I can get the same result by not specifying the hoverinfo argument, no hacking required. The challenge is that I don’t want to see the longitude and latitude of the points, as it is not really relevant.

According to the documentation, you can do this by specifying hoverinfo = 'text' although this is not working, FYI this used to work in a previous version of the shiny app, it seems that I have introduced a bug somewhere but I cant determine where.

Hello, were you able to find a workaround to this? I am unable to get the hover feature to work in the Plotly code on the website for Scatter Plots on Maps. This code only plots the markers with no hover functionality. Thanks,

library(plotly)
df <- read.csv(‘https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv’)

geo styling

g <- list(
scope = ‘usa’,
projection = list(type = ‘albers usa’),
showland = TRUE,
landcolor = toRGB(“gray95”),
subunitcolor = toRGB(“gray85”),
countrycolor = toRGB(“gray85”),
countrywidth = 0.5,
subunitwidth = 0.5
)

p <- plot_geo(df, lat = ~lat, lon = ~long) %>%
add_markers(
text = ~paste(airport, city, state, paste(“Arrivals:”, cnt), sep = “
”),
color = ~cnt, symbol = I(“square”), size = I(8), hoverinfo = “text”
) %>%
colorbar(title = “Incoming flights
February 2011”) %>%
layout(
title = ‘Most trafficked US airports
(Hover for airport)’, geo = g
)

Hey @neel123

It looks like there may be an issue with hoverinfo and plot_geo. I’ve opened an issue here https://github.com/ropensci/plotly/issues/1127