Home > Net >  Customize Hoverinfo in ggplotly/geom_sf()
Customize Hoverinfo in ggplotly/geom_sf()

Time:02-18

Problem

I am analyzing avalanche accidents in Switzerland and I created a map plot with ggplotly and geom_sf. I would now like to adjust the hoverinfo of the points in the plot, so that it also displays the variables year & place which are included in the dataframe but not included in aes() of the plot.

Data

head(df)

LV03_E LV03_N deaths caughts year canton                            place
1 602380 131230      2       2 1995     VS Chetseron / Vallon de l`Ertentse
2 586070 104160      1       1 1995     VS          Verbier / Les Ruinettes
3 731720 155110      1       4 1996     GR                    Chilchalphorn
4 575300 141010      1       2 1996     VD                 La L\xe9cherette
5 661640 202180      1       4 1996     OW  Pilatus / Matthorn / Ruessiflue
6 820490 206020      1       1 1996     GR          Alp Trida / Greitspitz 

Plot

ggplotly(
  ggplot()  
    geom_sf(data = swiss_cantons, fill = NA)  
    geom_point(data = df_avalanches_places, 
               mapping = aes(x = LV03_E, y=LV03_N, size = deaths), colour = "lightblue", alpha = 0.7)  
    theme_minimal())

enter image description here

Expected Result

The hoverinfo should be in the form:

place
year
deaths
caughts

What I tried

I tried to solve the problem by include the required variables in aes() and then use tooltip in ggplotly(), but I get an error message.

ggplotly(
  ggplot()  
    geom_sf(data = swiss_cantons, fill = NA)  
    geom_point(data = df_avalanches_places, 
               mapping = aes(x = LV03_E, y = LV03_N, size = deaths, place = place, year = year, caught = caughts), colour = "lightblue", alpha = 0.7)  
    theme_minimal(), tooltip = c('year', 'place', 'size', 'caught'))


Error in gsub("\n", br(), a, fixed = TRUE) : 
  input string 4 is invalid in this locale
In addition: Warning message:
Ignoring unknown aesthetics: place, year, caught 

How can I adjust the hoverinfo correctly?

CodePudding user response:

You can create a new column called tooltip to compose the text you want to show and then map this column to the aestetic:

library(ggplot2)
library(plotly)

plt <-
  iris %>%
  mutate(tooltip = paste(Species, "Petal Length:", Petal.Length, sep = "\n")) %>%
  ggplot(aes(Sepal.Length, Sepal.Width, tooltip = tooltip))  
  geom_point()

ggplotly(plt, tooltip = "tooltip")

enter image description here

I think this is not an issue to spatial maps in particular so I created this more minimal reproducible example.

CodePudding user response:

By default plolty tooltip correspond to actual aesthetics of the plot. Consider creating a new variable with the elements you want to show.

library(dplyr)
library(ggplot2)
library(plotly)
library(glue)

plot <- mtcars %>%
  mutate(
    model = rownames(.),
    label = glue::glue('{model} \nMPG:{mpg} \nWT: {wt}' )
    ) %>%
  ggplot(aes(x = wt, y = mpg, text = label))  
  geom_point()
  
ggplotly(plot, tooltip = 'text')

enter image description here

  • Related