Skip to content

Instantly share code, notes, and snippets.

@StaffanBetner
Created February 15, 2020 14:36
Show Gist options
  • Save StaffanBetner/76468e9a1b63d81e1a4dc2a983a2039b to your computer and use it in GitHub Desktop.
Save StaffanBetner/76468e9a1b63d81e1a4dc2a983a2039b to your computer and use it in GitHub Desktop.
library(pacman)
p_load(tidyverse, gsheet, ggforce)
timeline <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1S6dWcSNKVQK3tZkt-PvA55LkM8ZuOJykDntECYjExhk/edit?usp=sharing")
timeline %>% filter(Type=="Regentperiod") %>% select(Person, Tidsperiod) %>% distinct() -> tidsperioder
tidsperioder %>% select(Tidsperiod) %>% distinct %>% pull(Tidsperiod) -> tidsperioder_vector
timeline %>% filter(Type!="Regentperiod") %>% select(-Tidsperiod) %>% left_join(tidsperioder) %>%
full_join(timeline %>% filter(Type=="Regentperiod")) %>% mutate(Tidsperiod = factor(Tidsperiod, levels = tidsperioder_vector)) ->
timeline
timeline$Person <-
factor(timeline$Person,
levels = timeline %>% filter(Type=="Regentperiod") %>%
group_by(Person) %>%
summarise(Beginning = min(Beginning)) %>%
arrange(Beginning) %>%
ungroup %$% Person %>% rev, ordered=TRUE)
ggplot(data=timeline %>% filter(Type=="Levnadsperiod"),
aes(x=Person, ymin=Beginning, ymax=End, color="Levnadsperiod"))+
geom_linerange(size=2.5)+
labs(x=NULL, #caption = "@staffanbetner",
color=NULL,y=NULL, title="Sveriges regenter", caption="Efter Wikipediasidan \"Lista över Sveriges regenter\"")+
geom_linerange(data= timeline %>% filter(Type=="Regentperiod"),aes(color="Regentperiod"),size=2.5)+
scale_color_manual(values=c("Levnadsperiod"="#787878", "Regentperiod"="black"))+
theme(axis.text.y = element_text(size = 7),
strip.text.x = element_text(size = 7, margin = margin(0.05,0,0.05,0, "cm")),
strip.placement = "inside")+
scale_y_continuous(minor_breaks = seq(900,2000, 50), breaks=seq(900,2000, 100), limits=c(940,2020))+
coord_flip()+
facet_col(~Tidsperiod, scales = "free_y", space = "free")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment