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"))