Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Last active July 14, 2019 10:36
Show Gist options
  • Save mschnetzer/23ab15fc931a1892288c57084556f7c1 to your computer and use it in GitHub Desktop.
Save mschnetzer/23ab15fc931a1892288c57084556f7c1 to your computer and use it in GitHub Desktop.
Benutzung öffentlicher Verkehrsmittel nach Bundesländer (https://twitter.com/matschnetzer/status/1063423205401071618)
#### Code was adapted from https://github.com/ryantimpe/ChartOfTheDay ###
#### H/T to @ryantimpe!
library(tidyverse)
library(rvest)
library(png)
library(msthemes)
df <- tibble(geo=as.factor(c("Wien","Vorarlberg","Salzburg","Steiermark","Niederösterreich","Tirol","Oberösterreich","Kärnten","Burgenland")),
public=c(92,66,61,58,57,55,50,42,36),
ticket=as.factor(c("1","1","1","1","2","1","2","1","2")),
preis=c(365,378,1539,2285,NA,499,NA,2420,NA))
# Download png from https://www.onlinewebfonts.com/icon/10417
pic.raw <- readPNG("bus-and-train-silhouettes.png")
pic <- pic.raw[,,4] %>%
as.data.frame() %>%
mutate(y = n()-row_number()) %>%
gather(x, value, 1:(ncol(.)-1)) %>%
mutate(x = as.numeric(str_remove_all(x, "V"))) %>%
filter(value > 0) %>%
#Rescale to 50% of coords
group_by(x = x %/% 2, y = y %/% 2) %>%
summarize(value = max(value)) %>%
ungroup()
pic2 <- pic %>%
mutate(cluster = kmeans(.[, c(1:2)], 100)$cluster)
data <- df %>%
mutate(plot = map(public, function(public){
keep_k <- sample(1:100, public)
return(pic2 %>% filter(cluster %in% keep_k))
})) %>%
unnest(plot)
# Change order of facets by relevel geo
data$geo <- factor(data$geo, levels=df$geo[order(df$public)])
pub.labels <- df %>%
mutate(label_p = paste0(round(public), "%"))
ticket.labels <- df %>%
mutate(label_t = ifelse(!is.na(preis),paste0("Jahreskarte: ", preis," Euro"),paste0(" ")))
data %>%
ggplot(aes(x=x, y=y,fill=ticket)) +
geom_raster() +
ylim(10,220) +
scale_fill_manual(values = c("#2dd49c", "#ff4040"),
labels = c(" Bundesland "," Strecke "),
name = "Jahreskarte für ") +
coord_fixed() +
geom_text(data = pub.labels, aes(label = label_p),
x = 30, y = 20, size = 4,
color = "#00436b", fontface = "bold") +
geom_text(data = ticket.labels, aes(label = label_t),
x = 170, y = 17, size = 2.5) +
facet_wrap(~geo, ncol = 3) +
labs(title = "Wie stark wird öffentlicher Verkehr genutzt?",
subtitle = "Anteil der Bevölkerung, der öffentliche Verkehrsmittel nutzt",
caption = "Preise für 2018. Daten: VCÖ, Verkehrsverbunde. Grafik: @matschnetzer") +
theme_ms() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
strip.background = element_rect(fill = "#00436b"),
strip.text = element_text(color = "white", face = "bold", hjust = 0.5, size=10),
panel.background = element_rect(fill = "ivory"),
legend.position = "bottom"
) +
ggsave("publictrans.png", height = 6,width = 6, dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment