dly
dly

Reputation: 233

Two graphical representations in the same graph

I have a dataset of energy production for 2 types Renewable and Non-Renewable for 3 consecutive years.

I want to show the growth of Renewable energy production and Non-renewable production in 2 lines in the same facet for each country.

Target: For each country, in the same facet I want to observe which energy production(renewable or non-renewable) has a considerable growth.

> dput(top10ProducersRenwNonRenw_Mod)
structure(list(country = c("DE", "DE", "FR", "FR", "FR", "DE", 
"UK", "UK", "UK", "TR", "IT", "TR", "IT", "DE", "IT", "ES", "DE", 
"TR", "ES", "ES", "DE", "NO", "NO", "NO", "UA", "UA", "PL", "PL", 
"PL", "UA", "FR", "ES", "FR", "ES", "IT", "FR", "TR", "IT", "TR", 
"IT", "ES", "TR", "SE", "SE", "UK", "SE", "SE", "SE", "SE", "UK", 
"UK", "PL", "PL", "PL", "UA", "UA", "UA", "NO", "NO", "NO"), 
    country_name = c("Germany", "Germany", "France", "France", 
    "France", "Germany", "United Kingdom", "United Kingdom", 
    "United Kingdom", "Turkey", "Italy", "Turkey", "Italy", "Germany", 
    "Italy", "Spain", "Germany", "Turkey", "Spain", "Spain", 
    "Germany", "Norway", "Norway", "Norway", "Ukraine", "Ukraine", 
    "Poland", "Poland", "Poland", "Ukraine", "France", "Spain", 
    "France", "Spain", "Italy", "France", "Turkey", "Italy", 
    "Turkey", "Italy", "Spain", "Turkey", "Sweden", "Sweden", 
    "United Kingdom", "Sweden", "Sweden", "Sweden", "Sweden", 
    "United Kingdom", "United Kingdom", "Poland", "Poland", "Poland", 
    "Ukraine", "Ukraine", "Ukraine", "Norway", "Norway", "Norway"
    ), type2 = c("Non-Renewable", "Non-Renewable", "Non-Renewable", 
    "Non-Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable", 
    "Non-Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable", 
    "Non-Renewable", "Non-Renewable", "Renewable", "Non-Renewable", 
    "Non-Renewable", "Renewable", "Non-Renewable", "Non-Renewable", 
    "Non-Renewable", "Renewable", "Renewable", "Renewable", "Renewable", 
    "Non-Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable", 
    "Non-Renewable", "Non-Renewable", "Renewable", "Renewable", 
    "Renewable", "Renewable", "Renewable", "Renewable", "Renewable", 
    "Renewable", "Renewable", "Renewable", "Renewable", "Renewable", 
    "Renewable", "Non-Renewable", "Renewable", "Non-Renewable", 
    "Renewable", "Renewable", "Non-Renewable", "Renewable", "Renewable", 
    "Renewable", "Renewable", "Renewable", "Renewable", "Renewable", 
    "Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable"
    ), year = c("2016", "2017", "2017", "2018", "2016", "2018", 
    "2016", "2017", "2018", "2017", "2017", "2018", "2016", "2018", 
    "2018", "2017", "2017", "2016", "2016", "2018", "2016", "2018", 
    "2016", "2017", "2018", "2016", "2018", "2017", "2016", "2017", 
    "2018", "2016", "2016", "2018", "2018", "2017", "2018", "2016", 
    "2016", "2017", "2017", "2017", "2017", "2018", "2018", "2017", 
    "2018", "2016", "2016", "2017", "2016", "2017", "2018", "2016", 
    "2018", "2016", "2017", "2017", "2016", "2018"), ggwt_hours = c(471984, 
    449906, 448690.614, 447109.694, 445175.494, 393234.585, 268314.268, 
    253183.388, 239935.658, 200721.177, 200305.315, 195856.672, 
    190771, 185226.211, 185046, 182503, 175063, 174847, 164401, 
    163355.038, 147622, 146878.825, 146557, 146285, 145228.6, 
    141425, 138957.387, 137129.829, 137009, 134382.2, 113658.177, 
    103353, 99885.054, 99725.566, 96820, 93907.184, 93425.906, 
    90756.826, 87090, 86786.294, 84664, 83536.342, 82540, 80671.141, 
    79955.967, 78011, 77615.947, 77505, 75145, 73113.964, 58909.047, 
    18187.708, 15541.473, 15468, 13843.9, 12097, 12082.6, 3514, 
    3509, 3457.28)), row.names = c(NA, -60L), groups = structure(list(
    country = c("DE", "DE", "DE", "ES", "ES", "ES", "FR", "FR", 
    "FR", "IT", "IT", "IT", "NO", "NO", "NO", "PL", "PL", "PL", 
    "SE", "SE", "SE", "TR", "TR", "TR", "UA", "UA", "UA", "UK", 
    "UK", "UK"), country_name = c("Germany", "Germany", "Germany", 
    "Spain", "Spain", "Spain", "France", "France", "France", 
    "Italy", "Italy", "Italy", "Norway", "Norway", "Norway", 
    "Poland", "Poland", "Poland", "Sweden", "Sweden", "Sweden", 
    "Turkey", "Turkey", "Turkey", "Ukraine", "Ukraine", "Ukraine", 
    "United Kingdom", "United Kingdom", "United Kingdom"), year = c("2016", 
    "2017", "2018", "2016", "2017", "2018", "2016", "2017", "2018", 
    "2016", "2017", "2018", "2016", "2017", "2018", "2016", "2017", 
    "2018", "2016", "2017", "2018", "2016", "2017", "2018", "2016", 
    "2017", "2018", "2016", "2017", "2018"), .rows = structure(list(
        c(1L, 21L), c(2L, 17L), c(6L, 14L), c(19L, 32L), c(16L, 
        41L), c(20L, 34L), c(5L, 33L), c(3L, 36L), c(4L, 31L), 
        c(13L, 38L), c(11L, 40L), c(15L, 35L), c(23L, 59L), c(24L, 
        58L), c(22L, 60L), c(29L, 54L), c(28L, 52L), c(27L, 53L
        ), 48:49, c(43L, 46L), c(44L, 47L), c(18L, 39L), c(10L, 
        42L), c(12L, 37L), c(26L, 56L), c(30L, 57L), c(25L, 55L
        ), c(7L, 51L), c(8L, 50L), c(9L, 45L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 30L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Upvotes: 1

Views: 42

Answers (2)

akrun
akrun

Reputation: 887251

An approach with forcats

library(forcats)
library(dplyr)
library(ggplot2)
top10ProducersRenwNonRenw_Mod %>%
     mutate(year = as_factor(year)) %>%
     ggplot(aes(x=year , y=ggwt_hours,  color = country_name,
                                         group=country_name)) +
     geom_line()+
 
     facet_wrap(.~type2,scale='free')+
     xlab('Year')

-output

enter image description here

Upvotes: 2

Duck
Duck

Reputation: 39605

One approach can be next, where you can compare what happened with each country across the type of energy produced:

library(ggplot2)
#Code
ggplot(top10ProducersRenwNonRenw_Mod,aes(x=factor(year),y=ggwt_hours,color=country_name,
                                         group=country_name))+
  geom_line()+
  scale_y_continuous(labels = scales::comma)+
  facet_wrap(.~type2,scale='free')+
  xlab('Year')+
  theme_bw()

Output:

enter image description here

Upvotes: 2

Related Questions