Site icon R-bloggers

Three Ways to Include Images in Your ggplots

[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(
          weight = 600,
          _family = 'Source Sans Pro',
          _size = '32px'
        )
      )
      state_label <- htmltools::span(
        state,
        style = htmltools::css(
          _family = 'Source Sans Pro',
          _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.
  • Exit mobile version