plotly / plotly/plotly.R

style(hoveron="fills") doesn't work with ggplotly() when there are multiple polygons with the same fill color

Abierto
#1,641 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

difficulty: high ggplotly
Lenguaje dominante
R
Estrellas
2.7k
Forks
641
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

The following code based on the standard North Carolina shape-file fills counties by the values of BIR74, except that one county has been given an NA value for the variable.

library(ggplot2)
library(plotly)
library(sf)

nc_1 <-
  st_read(system.file("shape/nc.shp", package = "sf"), 
              quiet = TRUE) %>% 
  mutate(BIR74 = ifelse(NAME == "Ashe", NA, BIR74)) %>% 
  mutate(tooltip_text = ifelse(is.na(BIR74), 
                               paste0(NAME, " not reporting"), 
                               paste0(NAME, " reports ", BIR74)))
p_1 <-
  nc_1 %>% 
  ggplot() +
  geom_sf(aes(fill = BIR74, text = tooltip_text))

ggplotly(p_1, tooltip = "text") %>% 
  style(hoveron = "fills")

The custom tooltip indicates the missing value (hover over Ashe County) as expected.

The following code sets BIR74 to NA for two counties:

nc_2 <-
  st_read(system.file("shape/nc.shp", package = "sf"), 
          quiet = TRUE) %>% 
  mutate(BIR74 = ifelse(NAME %in% c("Ashe", "Alleghany"), NA, BIR74)) %>% 
  mutate(tooltip_text = ifelse(is.na(BIR74), 
                               paste0(NAME, " not reporting"), 
                               paste0(NAME, " reports ", BIR74)))
p_2 <-
  nc_2 %>% 
  ggplot() +
  geom_sf(aes(fill = BIR74, text = tooltip_text))

ggplotly(p_2, tooltip = "text") %>% 
  style(hoveron = "fills")

Hover over either Ashe or Alleghany and you get identical very long tooltips.

The output of plotly_json(p_2) indicates that Ashe and Alleghany have somehow been combined. See, e.g.:

json_2 <- plotly_json(p_2)
json_2$x$data %>%
  as.character() %>% 
  RJSONIO::fromJSON() %>% 
  .$data %>% 
  .[[100]] %>% 
  .[1:3]

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Ejecuta los dos ejemplos de North Carolina con ggplotly(), tooltip = "text" y style(hoveron = "fills"), y luego inspecciona plotly_json(p_2) como se muestra. Compara cómo representan los datos de los polígonos los casos con un NA y con dos NA; el trabajo estará terminado cuando Ashe y Alleghany muestren cada uno su propio tooltip en lugar de un único tooltip largo combinado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
r
Área
data-visualization
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.