-
Notifications
You must be signed in to change notification settings - Fork 2
/
tidytuesday_20200804.R
61 lines (54 loc) · 3.27 KB
/
tidytuesday_20200804.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# European Energy treemap
# TidyTuesday 2020 week 32
# Rebecca Stevick updated 8/04/2020
# Load libraries
library(tidyverse)
library(hrbrthemes)
library(treemapify)
# Load data
tuesdata <- tidytuesdayR::tt_load('2020-08-04')
# time to pipe, use energy_types
tuesdata$energy_types %>%
# combine the year columns into one
pivot_longer(names_to= "year", values_to="value", `2016`:`2018`) %>%
# remove missing data and select only year 2018 and level 1 types
drop_na(value, country_name) %>% filter(level=="Level 1" & year=="2018") %>%
# sum up the energy generated per country
group_by(country) %>% mutate(sumValue=sum(value)) %>% ungroup() %>%
# make a new column with only the top 20 generating countries, label the others as "Others"
mutate(countryOther=forcats::fct_lump_n(f=country_name, w=sumValue, other_level="All other \ncountries", n=20)) %>%
# time to plot
ggplot(aes(area = value, # area of each square is the energy generated per country/type
fill = factor(type, levels=unique(type)), # keep the order of energy so that Other is last
label = type, subgroup = countryOther))+
# add the tree map and add borders between countries
geom_treemap(color = "gray20") + geom_treemap_subgroup_border(color = "gray90", lwd=5)+
# change the fill colors - based on nationalparkcolors::park_palette("CraterLake", n=7)
scale_fill_manual(values = c("#4E7147", "#BE9C9D", "#376597","#7DCCD3", "#DBA662", "#9888A5", "#F7ECD8"))+
# edit the text colors and sizes
geom_treemap_text(aes(family=font_rc_light), colour = "gray75", place = "topleft", reflow = FALSE, size = 10)+
geom_treemap_subgroup_text(aes(family=font_rc), col = 'white',
# fit the text to the box so its size is relative to energy generation
grow = TRUE, padding.y = grid::unit(3, "mm"),padding.x = grid::unit(3, "mm")) +
# change theme and position the legend
theme_ipsum_rc()+theme(legend.position = c(0.78,1.1), legend.direction = "horizontal")+
# add labels
labs(fill=NULL, title="European Energy Generation in 2018",
subtitle="each area is proportional to energy generated per type & country in GWh (Gigawatt hours)
the top 20 countries are shown - all others are grouped together as \"All other countries\"",
caption="data from Eurostat Energy | plot by @rjstevick for #TidyTuesday")
# Saving -----------------------------
ggsave("EuropeanEnergy_plot.png", bg = "transparent", width = 12, height = 7, dpi = 400)
## try it with the treemap package as well
library(treemap)
tuesdata$energy_types %>%
pivot_longer(names_to= "year", values_to="value", `2016`:`2018`) %>%
drop_na(value, country_name) %>% filter(level=="Level 1" & year=="2018") %>%
group_by(country) %>% mutate(sumValue=sum(value)) %>% ungroup() %>%
mutate(countryOther=forcats::fct_lump_n(f=country_name, w=sumValue, other_level="All other countries", n=20)) %>%
treemap(index=c("countryOther", "type"),
vSize = "value", vColor="type", type="categorical",
palette=c("#4E7147", "#BE9C9D", "#376597","#7DCCD3", "#DBA662", "#9888A5", "#F7ECD8"),
overlap.labels = 0.4, fontsize.labels = c(18, 12),
title = "European Energy Generation in 2018",
align.labels=list(c("center", "top"), c("right", "bottom")))