Need to show the country flag

Viewed 1233

I have a dataset where energy production is represented in 2 facets. I want to show the flag of the relevant country at the end of each geom line and to the legend next to the country name.

Is there any way that I can fix this issue?

library(tidyverse) #install.packages("tidyverse)
library(tidymodels) #install.packages("tidymodels")
library(skimr)  #install.packages("skimr")
library(ggplot2)  #install.packages("ggplot2")
library(dplyr)  ##install.packages("deplyr")
library(knitr)  #install.packages("knitr")
library(tidyr)  #install.packages("tidyr")
library(viridis) #install.packages("viridis")
devtools::install_github("rensa/ggflags") #install.packages("devtools")
library(ggflags)

ggplot(categ_top10EnergyModf,
       aes(x = factor(year),
           y = ggwt_hours,
           color = country_name,
           group = country_name)) +
  geom_line(size = 1.5) +
  geom_point(size = 3) +
  geom_flag(country = country_name) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(. ~ type2, scale = 'free') +
  labs(x = "Year", 
       y = "Energy Production (GWh)",
       title = "Analysis of the growth of Renewable/Non-Renewable Energy production", 
       fill  = "Country", 
       color = "Country") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_grey()
> dput(head(categ_top10EnergyModf))
structure(list(country = c("DE", "DE", "FR", "FR", "FR", "DE"
), country_name = c("Germany", "Germany", "France", "France", 
"France", "Germany"), type2 = c("Non-Renewable", "Non-Renewable", 
"Non-Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable"
), year = c(2016L, 2017L, 2017L, 2018L, 2016L, 2018L), ggwt_hours = c(471984, 
449906, 448690.614, 447109.694, 445175.494, 393234.585)), row.names = c(NA, 
-6L), groups = structure(list(country = c("DE", "DE", "DE", "FR", 
"FR", "FR"), country_name = c("Germany", "Germany", "Germany", 
"France", "France", "France"), year = c(2016L, 2017L, 2018L, 
2016L, 2017L, 2018L), .rows = structure(list(1L, 2L, 6L, 5L, 
    3L, 4L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = c(NA, 6L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))
1 Answers

something like this?

You need to use tolower(country) to detect the name of the country. To have one unique legend you need to set the same name in colour and country in labs.

I noticed you set a setting for theme, but to make it work you need to set it after theme_gray, otherwise it gets overwritten.

To set the flags just at the end you need to set x and y in aes of geom_flag to be equal only to the last point. That's what the dplyr pipeline is for at the beginning.

With your full dataset, the visualization should be better.

library(ggplot2)
library(dplyr)
library(ggflags)

categ_top10EnergyModf %>%
 mutate(year = factor(year),
        country = tolower(country)) %>% 
 group_by(type2, country) %>% 
 mutate(country_x = max(levels(year)),
        country_y = ggwt_hours[country_x == year]) %>% 
 
 ggplot(aes(x = year,
            y = ggwt_hours,
            color = country,
            group = country)) +
 geom_line(size = 1.5) +
 geom_point(size = 3) +
 geom_flag(aes(x = country_x, 
               y = country_y, 
               country = country)) +
 scale_y_continuous(labels = scales::comma) +
 facet_wrap(. ~ type2, scale = 'free') +
 labs(x = "Year", 
      y = "Energy Production (GWh)",
      title = "Analysis of the growth of Renewable/Non-Renewable Energy production", 
      country = "Country", 
      color   = "Country") +
 theme_grey() +
 theme(plot.title = element_text(hjust = 0.5)) +
 scale_country()

enter image description here

Related