TidyTuesday 2020 Week32

European Energy

Preparations

library(tidyverse)
library(ggrepel)
library(flair) #Highlight, Annotate, and Format your R Source Code

Import

energy_types <- readr::read_csv(
  'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/energy_types.csv',
  col_types = cols(
    country = col_character(),
    country_name = col_character(),
    type = col_character(),
    level = col_character(),
    `2016` = col_double(),
    `2017` = col_double(),
    `2018` = col_double()
  )
)

Tidy & Transform

  • rowSums()1
  • across()2
(
enegy_summary <-
energy_types %>%
  filter(level == 'Level 1') %>%
  select(country, type, `2018`) %>%
  mutate(category = case_when(
      type == 'Conventional thermal' ~ 'Conventional thermal',
      type == 'Nuclear' ~ 'Nuclear',
      TRUE ~ 'Renewable'
      )
    ) %>%
  group_by(country, category) %>%
  summarise(`2018` = sum(`2018`), .groups = 'drop') %>%
  pivot_wider(names_from = category, values_from = `2018`) %>%
  mutate(total = rowSums(across(where(is.numeric)))) %>%
  mutate(across(where(is.numeric) & !total, ~ . / total)) %>%
  arrange(`Conventional thermal`) %>%
  mutate(
    xmax = cumsum(total),
    xmin = xmax - total
  )
)

## # A tibble: 37 x 7
##    country `Conventional thermal` Nuclear Renewable   total    xmax    xmin
##    <chr>                    <dbl>   <dbl>     <dbl>   <dbl>   <dbl>   <dbl>
##  1 AL                      0        0         1       8597.   8597.      0 
##  2 NO                      0.0235   0         0.976 146845. 155442.   8597.
##  3 SE                      0.0939   0.416     0.490 158287. 313729. 155442.
##  4 FR                      0.0978   0.713     0.190 551686. 865415. 313729.
##  5 GE                      0.174    0         0.826  12149. 877563. 865415.
##  6 LU                      0.214    0         0.786   2165. 879728. 877563.
##  7 SK                      0.229    0.582     0.189  23717  903445. 879728.
##  8 AT                      0.249    0         0.751  65588. 969033. 903445.
##  9 LT                      0.252    0         0.748   3183. 972216. 969033.
## 10 SI                      0.308    0.359     0.333  15284. 987500. 972216.
## # … with 27 more rows

Visualise

  • sec.axis3
  • geom_label_repel()4
set.seed(12)

enegy_summary %>%
  ggplot() +
    geom_rect(
      aes(xmin = xmin, xmax=xmax, ymin = 0, ymax = Nuclear),
      fill = 'lightgoldenrod2', color = 'grey50'
    ) +
    geom_rect(
      aes(xmin = xmin, xmax=xmax, ymin = Nuclear, ymax = Nuclear+Renewable),
      fill = 'goldenrod2', color = 'grey50'
    ) +
    geom_rect(
      aes(xmin = xmin, xmax=xmax, ymin = 0, ymax = -1*`Conventional thermal`),
      fill = 'grey85', color = 'grey50'
    ) +
    geom_label_repel(
      aes(x = (xmax + xmin) / 2, y = (Nuclear+Renewable) * 0.8, label = country),
      direction = "x",
      nudge_y = 1,
      force = 0.5,
      arrow = arrow(length = unit(0.015, "npc"), ends = "first", type = "closed"),
      color = 'cadetblue4'
    ) +
    scale_x_continuous('', breaks = NULL, labels = NULL) +
    scale_y_continuous(
      '',
      breaks = seq(1, -1, length.out = 9),
      labels = c("", "", "", "", '0%', '25%', '50%', '75%', '100%'),
      sec.axis = dup_axis(
        labels = rev(c("", "", "", "", '0%', '25%', '50%', '75%', '100%'))
      )
    ) +
    labs(
      title = 'How european countries generated electricity in 2018',
      caption = 'Inspiration: Karim Douïeb | #TidyTuesday | @mstkolf'
    ) +
    theme_minimal() +
    theme(
      plot.title = element_text(face = "bold", size = 35, family = "serif"),
      plot.caption = element_text(size = 15)
    )

References

Bodwin, Kelly, and Hunter Glanz. 2020. Flair: Highlight, Annotate, and Format Your R Source Code. https://CRAN.R-project.org/package=flair.

Slowikowski, Kamil. 2020. Ggrepel: Automatically Position Non-Overlapping Text Labels with ’Ggplot2’. https://CRAN.R-project.org/package=ggrepel.

Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2020. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.

Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2020. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.

Reproducibility

## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.0 (2020-04-24)
##  os       macOS Catalina 10.15.5      
##  system   x86_64, darwin17.0          
##  ui       X11                         
##  language (EN)                        
##  collate  ja_JP.UTF-8                 
##  ctype    ja_JP.UTF-8                 
##  tz       Asia/Tokyo                  
##  date     2020-08-22                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date       lib source        
##  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
##  backports     1.1.7   2020-05-13 [1] CRAN (R 4.0.0)
##  blob          1.2.1   2020-01-20 [1] CRAN (R 4.0.0)
##  blogdown      0.20    2020-06-23 [1] CRAN (R 4.0.2)
##  bookdown      0.20    2020-06-23 [1] CRAN (R 4.0.2)
##  broom         0.7.0   2020-07-09 [1] CRAN (R 4.0.2)
##  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.0.0)
##  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
##  colorspace    1.4-1   2019-03-18 [1] CRAN (R 4.0.0)
##  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
##  curl          4.3     2019-12-02 [1] CRAN (R 4.0.0)
##  DBI           1.1.0   2019-12-15 [1] CRAN (R 4.0.0)
##  dbplyr        1.4.4   2020-05-27 [1] CRAN (R 4.0.0)
##  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
##  dplyr       * 1.0.0   2020-05-29 [1] CRAN (R 4.0.0)
##  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
##  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
##  farver        2.0.3   2020-01-16 [1] CRAN (R 4.0.0)
##  flair       * 0.0.2   2020-04-23 [1] CRAN (R 4.0.2)
##  forcats     * 0.5.0   2020-03-01 [1] CRAN (R 4.0.0)
##  fs            1.4.1   2020-04-04 [1] CRAN (R 4.0.0)
##  generics      0.0.2   2018-11-29 [1] CRAN (R 4.0.0)
##  ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 4.0.2)
##  ggrepel     * 0.8.2   2020-03-08 [1] CRAN (R 4.0.2)
##  glue          1.4.1   2020-05-13 [1] CRAN (R 4.0.0)
##  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
##  haven         2.3.0   2020-05-24 [1] CRAN (R 4.0.0)
##  here          0.1     2017-05-28 [1] CRAN (R 4.0.2)
##  hms           0.5.3   2020-01-08 [1] CRAN (R 4.0.0)
##  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.2)
##  httr          1.4.1   2019-08-05 [1] CRAN (R 4.0.0)
##  jsonlite      1.6.1   2020-02-02 [1] CRAN (R 4.0.0)
##  knitr         1.28    2020-02-06 [1] CRAN (R 4.0.0)
##  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
##  lubridate     1.7.8   2020-04-06 [1] CRAN (R 4.0.0)
##  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
##  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.0.0)
##  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
##  pillar        1.4.4   2020-05-05 [1] CRAN (R 4.0.0)
##  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
##  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
##  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
##  Rcpp          1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
##  readr       * 1.3.1   2018-12-21 [1] CRAN (R 4.0.0)
##  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.0.0)
##  reprex        0.3.0   2019-05-16 [1] CRAN (R 4.0.0)
##  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
##  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.2)
##  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
##  rstudioapi    0.11    2020-02-07 [1] CRAN (R 4.0.0)
##  rvest         0.3.5   2019-11-08 [1] CRAN (R 4.0.0)
##  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.0)
##  sessioninfo * 1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
##  stringi       1.4.6   2020-02-17 [1] CRAN (R 4.0.0)
##  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
##  tibble      * 3.0.3   2020-07-10 [1] CRAN (R 4.0.2)
##  tidyr       * 1.1.0   2020-05-20 [1] CRAN (R 4.0.0)
##  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.0)
##  tidyverse   * 1.3.0   2019-11-21 [1] CRAN (R 4.0.0)
##  utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.0)
##  vctrs         0.3.1   2020-06-05 [1] CRAN (R 4.0.0)
##  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
##  xfun          0.14    2020-05-20 [1] CRAN (R 4.0.0)
##  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.0.0)
##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
## 
## [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
comments powered by Disqus

関連項目