Last active
December 18, 2024 13:44
-
-
Save ikashnitsky/8cc26eab8165b0b79f67da761aa66a1e to your computer and use it in GitHub Desktop.
Code accompanying Rotate the Damn Plot post -- https://ikashnitsky.phd/2024/rotate-damn-plot/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0.9505599562960936 | 7.373295920141045 | |
---|---|---|
1.0986069379950836 | 7.328921556455017 | |
1.2791586998087952 | 7.3965756996349725 | |
1.4164162797049977 | 7.896948176106876 | |
1.5681507784758253 | 7.501837550594721 | |
1.992078667030866 | 7.42089841325023 | |
2.1416279704998624 | 7.585483350301706 | |
2.2817536192297174 | 7.757505400908842 | |
2.4356733133023756 | 7.802699212833056 | |
2.624146408085222 | 7.653956941719848 | |
3.023354274788309 | 7.4983238558764365 | |
3.1529636711281053 | 7.70763080132105 | |
3.3053810434307573 | 8.04386531250776 | |
3.461076208686151 | 8.290556479849023 | |
3.636028407538923 | 7.761180502098284 | |
4.049713193116636 | 7.710064314270815 | |
4.183419830647363 | 7.807442079908618 | |
4.343758535919147 | 7.9272801768021655 | |
4.489210598197214 | 8.20379429366045 | |
4.664299371756352 | 7.920687343249485 | |
5.0737503414367655 | 7.735231307889052 | |
5.25839934444141 | 7.690956271262198 | |
5.352909041245562 | 8.10912319038514 | |
5.522261677137393 | 8.482717091703707 | |
5.685878175361922 | 8.013011844751807 | |
6.096148593280525 | 7.805169973430012 | |
6.252936356186833 | 8.022013359489458 | |
6.397022671401257 | 8.335837202950014 | |
6.545889101338433 | 8.269077003302623 | |
6.726577437858508 | 8.083000173822354 | |
7.118819994537013 | 7.867646693650518 | |
7.29022125102431 | 7.93527600506568 | |
7.434034416826002 | 8.256561793846688 | |
7.589183283255942 | 8.518176851828859 | |
7.743512701447693 | 8.302177745772392 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=============================================================================== | |
# 2024-12-12 -- blog | |
# Rotate the damn plot | |
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com | |
#=============================================================================== | |
# The original plot | |
# https://www.linkedin.com/feed/update/urn:li:activity:7271909125997924353/ | |
library(tidyverse) | |
# Points digitized manually using | |
# https://web.eecs.utk.edu/~dcostine/personal/PowerDeviceLib/DigiTest/index.html | |
digi <- read_csv( | |
"https://gist.githubusercontent.com/ikashnitsky/8cc26eab8165b0b79f67da761aa66a1e/raw/8d44c9e6ddea51de3cbb0fdb742fb75a62a307fd/data.csv", | |
col_names = c("x", "happiness") | |
) | |
# get all combinations of the categories and attach the digitized data | |
df <- crossing( | |
country = c("United Kingdom", "Estonia", "Denmark", "Netherlands", "Czech Republic", "Finland", "Austria") |> as_factor(), | |
group = c("18-29", "30-39", "40-49", "50-59", "Total") | |
) |> | |
mutate(happiness = digi |> pull(happiness)) | |
# recreate the plot | |
df |> | |
ggplot(aes(x = country, y = happiness, fill = group))+ | |
geom_col(position = position_dodge()) + | |
labs( | |
x = NULL, | |
y = NULL, | |
fill = NULL | |
) + | |
coord_cartesian(ylim = c(6, NA), expand = 0)+ | |
scale_fill_manual(values = c("#2f74cc", "#ff7502", "#a3d384", "#ffbc00", "#908d90"))+ | |
theme_minimal()+ | |
theme( | |
legend.position = "bottom", | |
panel.grid.minor = element_blank() | |
) | |
# custom theming | |
devtools::source_gist("653e1040a07364ae82b1bb312501a184") | |
sysfonts::font_add_google("Atkinson Hyperlegible", family = "ah") | |
theme_set(theme_ik(base_family = "ah")) | |
# Create the plot | |
df |> | |
ggplot(aes(y = country, x = happiness, color = group))+ | |
geom_point(size = 2) + | |
labs( | |
title = "How happy are you with your home?", | |
subtitle = "Happiness scores from Generations and Gender Programme surveys\non a scale of 0 to 10", | |
x = NULL, | |
y = NULL, | |
color = "Age group" | |
) + | |
scale_color_manual(values = c("#2f74cc", "#ff7502", "#a3d384", "#ffbc00", "#908d90"))+ | |
theme( | |
legend.position = "bottom", | |
panel.grid.minor.x = element_blank() | |
)+ | |
scale_x_continuous(position = "top") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment