Home > Software design >  Dodge country flags at the end of geom_line using geom_flag
Dodge country flags at the end of geom_line using geom_flag

Time:09-11

I'm trying to create a plot where the last point of a geom_line() is a picture of the flag of the respective country. Using other posts, I have managed to create the following graph so far:

enter image description here

In the large dataset I have, the country flags sometimes overlap and are indiscernible from each other. Is there a way to display them (ie the last point of the geom_line) dodged? Or does anyone have a suggestion to make the flags more visible in general?

I tried searching through other answers but I see that the dodge is only available on vertical plots so I don't think the coord_flip trick would work for me. Below I've included some sample data as well as the code used to produce the graph. Thanks in advance!

library(tidyverse)
library(ggflags)
set.seed(123)
data <- data.frame(iso2c = c("BT", "IN", "LK", "NP",
                             "BT", "IN", "LK", "NP"),
                   Year = c(2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011),
                   degree_norm = runif(8))

data %>%
  mutate(Year = factor(Year),
         iso2c = tolower(iso2c)) %>% 
  group_by(iso2c) %>% 
  mutate(country_x = max(levels(Year)), 
         country_y = degree_norm[country_x == Year]) %>%
  ggplot(aes(x = Year,
             y = degree_norm,
             color = iso2c,
             group = iso2c))  
  geom_line(aes(linetype = iso2c), size = 1.25)  
  # geom_point(size = 3)  
  geom_flag(aes(x = country_x,
                y = country_y, 
                country = iso2c))  
  scale_y_continuous(labels = scales::comma)  
  theme_grey()  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.key = element_blank())  
  scale_color_discrete(breaks  = c("bt", "in", "lk", "np"), 
                       labels = c("Bhutan", "India", "Sri Lanka","Nepal"),
                       name="Country")   
  scale_linetype_discrete(breaks  = c("bt", "in", "lk", "np"), 
                          labels = c("Bhutan", "India", "Sri Lanka","Nepal"),
                          name="Country")  
  theme(legend.key.width=unit(3,"line"))  
  labs(x = "Year", 
       y = "Normalised Degree Centrality",
       country = "Country", 
       color   = "Country"
  ) 

CodePudding user response:

This is relatively simple, using position_nudge to shift it slightly.

library(tidyverse)
library(ggflags) # Installed from https://github.com/jimjam-slam/ggflags
set.seed(123)
data <- data.frame(iso2c = c("BT", "IN", "LK", "NP",
                             "BT", "IN", "LK", "NP"),
                   Year = c(2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011),
                   degree_norm = runif(8))

data %>%
  mutate(Year = factor(Year),
         iso2c = tolower(iso2c)) %>% 
  group_by(iso2c) %>% 
  mutate(country_x = max(levels(Year)), 
         country_y = degree_norm[country_x == Year]) %>%
  ggplot(aes(x = Year,
             y = degree_norm,
             color = iso2c,
             group = iso2c))  
  geom_line(aes(linetype = iso2c), size = 1.25)  
  # geom_point(size = 3)  
  geom_flag(aes(x = country_x,
                y = country_y, 
                country = iso2c), 
            position = position_nudge(x = 0.1))   # You can change the amount of nudge here
  scale_y_continuous(labels = scales::comma)  
  theme_grey()  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.key = element_blank())  
  scale_color_discrete(breaks  = c("bt", "in", "lk", "np"), 
                       labels = c("Bhutan", "India", "Sri Lanka","Nepal"),
                       name="Country")   
  scale_linetype_discrete(breaks  = c("bt", "in", "lk", "np"), 
                          labels = c("Bhutan", "India", "Sri Lanka","Nepal"),
                          name="Country")  
  theme(legend.key.width=unit(3,"line"))  
  labs(x = "Year", 
       y = "Normalised Degree Centrality",
       country = "Country", 
       color   = "Country"
  ) 

Created on 2022-09-10 by the reprex package (v2.0.1)

  • Related