2

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"))
Edo
  • 7,567
  • 2
  • 9
  • 19
dly
  • 233
  • 1
  • 11

1 Answers1

4

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

Edo
  • 7,567
  • 2
  • 9
  • 19
  • Edo, thank you for the answer. I tried but I'm getting this error. Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘grobify’ for signature ‘"NULL"’ – dly Oct 07 '20 at 10:24
  • use country not country_name – Edo Oct 07 '20 at 11:12
  • I used the country but it works without scale_country().when I add scale_country(), the above issue throws because UK flag is missing. So flags are not coming for the legend – dly Oct 07 '20 at 12:32
  • 2
    because `uk` needs to be `gb` to be recognized by `geom_flag`. add this at the beginning of your pipeline: `mutate(country = if_else(country == "UK", "GB", country))` – Edo Oct 07 '20 at 14:51
  • check out rensa's github page to learn all the accepted names: https://github.com/rensa/ggflags – Edo Oct 07 '20 at 14:52