davidgohel / ggiraph

make 'ggplot' graphics interactive

Home Page:https://davidgohel.github.io/ggiraph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

geom_hline_interactive with coord_polar not working

dominicroye opened this issue · comments

I wanted to make an interactive graph with geom_hline using a polar coordinate system. With the cartesian CS, it works fine, but in the polar CS, it shows only the last value, in this example 2020.

gg <- ggplot() + 
          geom_hline_interactive(aes(yintercept = 2000:2020,
                                     colour = 2000:2020,
                                      tooltip = 2000:2020),
                                 size = 2)
girafe(ggobj = gg)

gg <- ggplot() + 
  geom_hline_interactive(aes(yintercept = 2000:2020, 
                                  colour = 2000:2020,
                             tooltip = 2000:2020),
                         size = 2) +
  coord_polar()
  
girafe(ggobj = gg)

My session info:
`> sessionInfo()
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.utf8 LC_CTYPE=Spanish_Spain.utf8
[3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.utf8

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base

other attached packages:
[1] ggiraph_0.8.9 geomtextpath_0.1.3 ggtext_0.1.2
[4] mapSpain_0.9.0 lubridate_1.9.3 forcats_1.0.0
[7] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[10] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[13] ggplot2_3.5.0 tidyverse_2.0.0 sf_1.0-16
[16] terra_1.7-71

loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 farver_2.1.1
[3] fastmap_1.1.1 fontquiver_0.2.1
[5] promises_1.3.0 digest_0.6.35
[7] timechange_0.3.0 mime_0.12
[9] lifecycle_1.0.4 gfonts_0.2.0
[11] magrittr_2.0.3 compiler_4.3.3
[13] rlang_1.1.3 tools_4.3.3
[15] yaml_2.3.8 utf8_1.2.4
[17] labeling_0.4.3 htmlwidgets_1.6.4
[19] classInt_0.4-10 curl_5.2.1
[21] xml2_1.3.6 mapproj_1.2.11
[23] KernSmooth_2.23-22 httpcode_0.3.0
[25] withr_3.0.0 grid_4.3.3
[27] fansi_1.0.6 gdtools_0.3.7
[29] xtable_1.8-4 e1071_1.7-14
[31] colorspace_2.1-0 scales_1.3.0
[33] pals_1.8 dichromat_2.0-0.1
[35] crul_1.4.2 cli_3.6.2
[37] crayon_1.5.2 ragg_1.3.0
[39] generics_0.1.3 rstudioapi_0.16.0
[41] tzdb_0.4.0 commonmark_1.9.1
[43] DBI_1.2.2 proxy_0.4-27
[45] maps_3.4.2 vctrs_0.6.5
[47] jsonlite_1.8.8 fontBitstreamVera_0.1.1
[49] hms_1.1.3 systemfonts_1.0.6
[51] units_0.8-5 glue_1.7.0
[53] codetools_0.2-19 stringi_1.8.3
[55] gtable_0.3.4 later_1.3.2
[57] munsell_0.5.1 pillar_1.9.0
[59] rappdirs_0.3.3 htmltools_0.5.8.1
[61] R6_2.5.1 textshaping_0.3.7
[63] shiny_1.8.1.1 markdown_1.12
[65] gridtext_0.1.5 fontLiberation_0.1.0
[67] httpuv_1.6.15 class_7.3-22
[69] Rcpp_1.0.12 uuid_1.2-0
[71] xfun_0.43 pkgconfig_2.0.3 `

The issue here is that last circle shape (2020) is above all others. You can work around that by sorting the data by z ascending:

library(ggiraph)
library(tidyverse)

df <- data.frame(
  z = c(2020:2000)
) |> mutate(
  str = as.character(z)
)

gg <- ggplot(data = df) +
  geom_hline_interactive(
    aes(
      yintercept = z,
      colour = z,
      tooltip = str
    ),
    linewidth = 1
  ) +
  scale_y_continuous(limits = c(1995, 2020)) +
  coord_polar()

girafe(ggobj = gg)
Capture d’écran 2024-04-11 à 23 43 04

Thanks! It worked.

I'm sorry. I just found something strange. It appears that ggplot is drawing additional lines with NA values. Do you know about this? It seems only to happen with coord_polar().

library(tidyverse)
library(ggiraph)

df <- data.frame(
  z = c(2020:1990)
) |> mutate(
  str = as.character(z)
)

gg <- ggplot(data = df) +
  geom_hline_interactive(
    aes(
      yintercept = z,
      colour = z,
      tooltip = str
    ),
    linewidth = 1
  ) +
  scale_y_continuous() +
  coord_polar()

girafe(ggobj = gg)

image

Yep, and for example 1994 is removed. That's why I added a limit (you removed it), I think ggplot or grid is cropping the shapes that are overlaid.

Without ggiraph, I can only see 10 circles instead of 11.

library(tidyverse)
library(ggiraph)
df <- data.frame(
  z = c(2020:2010)
) |> mutate(
  str = as.character(z)
)

gg <- ggplot(data = df) +
  geom_hline(
    aes(
      yintercept = z,
      colour = z
    ),
    linewidth = 1
  ) + 
  scale_y_continuous() +
  coord_polar()
gg
Capture d’écran 2024-04-12 à 19 25 16