[This article was first published on Albert Rapp, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
library(tidyverse)library(giscoR)library(ggiraph)germany\_districts <- gisco\_get\_nuts( year = "2021", nuts\_level = 3, epsg = 3035, country = 'Germany') |> as\_tibble() |> janitor::clean\_names()germany\_states <- gisco\_get\_nuts( year = "2021", nuts\_level = 1, epsg = 3035, country = 'Germany') |> as\_tibble() |> janitor::clean\_names()ggplt <- germany\_districts |> ggplot(aes(geometry = geometry)) + geom\_sf( data = germany\_states, aes(fill = nuts\_name), color = 'black', linewidth = 0.5 ) + geom\_sf\_interactive( fill = NA, aes( data\_id = nuts\_id, tooltip = glue::glue('{nuts\_name}') ), linewidth = 0.1 ) + theme\_void() + theme( legend.position = 'none' )girafe(ggobj = ggplt)
library(sf)state\_nmbrs <- map\_dbl( germany\_districts$geometry, \(x) { map\_lgl( germany\_states$geometry, \(y) st\_within(x, y) |> as.logical() ) |> which() }) germany\_districts\_w\_state <- germany\_districts |> mutate( state = germany\_states$nuts\_name[state\_nmbrs] )ggplt <- germany\_districts\_w\_state |> ggplot(aes(geometry = geometry)) + geom\_sf( data = germany\_states, aes(fill = nuts\_name), color = 'black', linewidth = 0.5 ) + geom\_sf\_interactive( fill = NA, aes( data\_id = nuts\_id, tooltip = glue::glue('{nuts\_name}<br>{state}') ), linewidth = 0.1 ) + theme\_void() + theme( legend.position = 'none' )girafe(ggobj = ggplt)
Nicer tooltip and nicer colors
make\_nice\_label <- function(nuts\_name, state) { nuts\_name\_label <- htmltools::span( nuts\_name, style = htmltools::css( fontweight = 600, font\_family = 'Source Sans Pro', font\_size = '32px' ) ) state\_label <- htmltools::span( state, style = htmltools::css( font\_family = 'Source Sans Pro', font\_size = '20px' ) ) glue::glue('{nuts\_name\_label}<br>{state\_label}')}ggplt <- germany\_districts\_w\_state |> mutate( nice\_label = map2\_chr( nuts\_name, state, make\_nice\_label ) ) |> ggplot(aes(geometry = geometry)) + geom\_sf( data = germany\_states, aes(fill = nuts\_name), color = 'black', linewidth = 0.5 ) + geom\_sf\_interactive( fill = NA, aes( data\_id = nuts\_id, tooltip = nice\_label ), linewidth = 0.1 ) + theme\_void() + theme( legend.position = 'none' ) + scale\_fill\_manual( values = c("#A0CBE8FF", "#F28E2BFF", "#FFBE7DFF", "#59A14FFF", "#8CD17DFF", "#B6992DFF", "#F1CE63FF", "#499894FF", "#86BCB6FF", "#E15759FF", "#FF9D9AFF", "#79706EFF", "#BAB0ACFF", "#D37295FF", "#FABFD2FF", "#B07AA1FF", "#D4A6C8FF", "#9D7660FF", "#D7B5A6FF") )girafe(ggobj = ggplt)
To leave a comment for the author, please follow the link and comment on their blog: Albert Rapp.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: Three Ways to Include Images in Your ggplots