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
# Autor: Taivo Pungas (github.com/taivop) | |
# Litsents: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) | |
library(ggplot2) | |
library(dplyr) | |
library(reshape2) | |
library(opendata.ee) | |
dfs <- data_fin_struktuuritoetus() | |
data <- dfs %>% | |
mutate(projekti_algus = as.Date(projekti_algus, "%d.%m.%Y")) %>% | |
mutate(aastakuu=as.Date(format(projekti_algus, "%Y-%m-01"), "%Y-%m-%d")) %>% | |
group_by(aastakuu) %>% | |
summarise(toetuste_summa=sum(projekti_toetus), | |
projektide_arv=n()) | |
correct_label_x <- xlab("Projekti algusaeg") | |
correct_label_y <- ylab("Rahastatud projektide arv [projekte/kuu]") | |
correct_title <- ggtitle("EL struktuurifondidest rahastatud projektide arv 2004-2015") | |
correct_theme <- theme_bw() + | |
theme(panel.grid.minor=element_blank(), | |
text=element_text(size=16)) | |
# ---- Nice graph ---- | |
ggplot(data) + | |
geom_line(aes(x=aastakuu, y=projektide_arv)) + | |
correct_label_x + correct_label_y + correct_title + | |
correct_theme | |
# ---- No axis labels ---- | |
ggplot(data) + | |
geom_line(aes(x=aastakuu, y=projektide_arv), size=1) + | |
xlab("") + ylab("") + correct_title + | |
correct_theme | |
ggsave("graphs/no_labels.svg", width=16, height=9) | |
# ---- Overloaded with information ---- | |
data2 <- dfs %>% | |
mutate(projekti_algus = as.Date(projekti_algus, "%d.%m.%Y")) %>% | |
mutate(aastakuu=as.Date(format(projekti_algus, "%Y-%m-01"), "%Y-%m-%d")) %>% | |
group_by(aastakuu, projekti_maakond) %>% | |
summarise(toetuste_summa=sum(projekti_toetus), | |
projektide_arv=n()) %>% | |
mutate(Maakond=projekti_maakond) | |
ggplot(data2) + | |
geom_line(aes(x=aastakuu, y=projektide_arv, | |
color=Maakond), size=1) + | |
correct_label_x + correct_label_y + | |
ggtitle("EL struktuurifondidest rahastatud projektide arv maakondade kaupa, 2004-2015") + | |
correct_theme | |
ggsave("graphs/overloaded_info.svg", width=16, height=9) | |
g_split <- ggplot(data2) + | |
geom_line(aes(x=aastakuu, y=projektide_arv, | |
color=Maakond), size=1) + | |
correct_label_x + correct_label_y + | |
ggtitle("EL struktuurifondidest rahastatud projektide arv maakondade kaupa, 2004-2015") + | |
facet_wrap(~Maakond, ncol=3) + | |
correct_theme + | |
theme(legend.position="none") | |
g_split | |
ggsave("graphs/overloaded_info_split.svg", width=16, height=32) | |
g_split_rescaled <- | |
ggplot(data2) + | |
geom_line(aes(x=aastakuu, y=projektide_arv, | |
color=Maakond), size=1) + | |
correct_label_x + correct_label_y + | |
ggtitle("EL struktuurifondidest rahastatud projektide arv maakondade kaupa, 2004-2015") + | |
facet_wrap(~Maakond, ncol=3, scales="free_y") + | |
correct_theme + | |
theme(legend.position="none") | |
g_split_rescaled | |
ggsave("graphs/overloaded_info_split_rescaled.svg", width=16, height=32) | |
# ---- Overloaded with elements ---- | |
ggplot(data) + | |
geom_line(aes(x=aastakuu, y=projektide_arv)) + | |
correct_label_x + correct_label_y + correct_title + | |
correct_theme + | |
theme(panel.grid.minor=element_line(color="black"), | |
panel.grid.major=element_line(color="black"), | |
text=element_text(family="Didot")) | |
ggsave("graphs/overloaded_elements.svg", width=16, height=9) | |
# ---- Axes cut off misleadingly ---- | |
# https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(PPP)_per_capita | |
countries <- c("Eesti", "Poola", "Läti", "Kreeka", "Ungari", "Venemaa") | |
gdps <- c(28650, 26499, 24652, 26391, 26275, 25965) | |
data3 <- data.frame(Country=countries, GDP_PPP=gdps) | |
g_gdp <- ggplot(data3) + | |
geom_bar(aes(x=Country, y=GDP_PPP, fill=Country), stat="identity") + | |
correct_theme + ylab("SKP ostujõu pariteedi alusel [$]") + xlab("Riik") + | |
theme(legend.position="none") | |
ggtitle("SKP ostujõu pariteedi alusel inimese kohta, 2015 (IMF andmed)") | |
g_gdp | |
ggsave("graphs/cutoffaxis_good.svg", width=16, height=9) | |
g_gdp + | |
coord_cartesian(ylim=c(24000, 30000)) | |
ggsave("graphs/cutoffaxis_bad.svg", width=16, height=9) | |
ggsave("graphs/cutoffaxis_bad.png", width=16, height=9) | |
# ---- Sampling frequency ---- | |
years <- seq(1994, 2016, by=4) | |
revenues <- c(79, 94, 102, 112, 98, 104) * 1000 | |
data4 <- data.frame(Year=years, Revenue=revenues) | |
g_freq <- ggplot(data4) + | |
geom_smooth(aes(x=Year, y=Revenue), size=2, color="blue") + | |
ylim(0, NA) + | |
ylab("Aastatulu [$]") + xlab("Aasta") + | |
correct_theme | |
g_freq | |
ggsave("graphs/freq_bad.svg", width=16, height=9) | |
g_freq + | |
geom_point(aes(x=Year, y=Revenue), color="blue", size=3) | |
ggsave("graphs/freq_better.svg", width=16, height=9) | |
g_freq_good <- ggplot(data4) + | |
geom_line(aes(x=Year, y=Revenue), size=2, color="blue") + | |
geom_point(aes(x=Year, y=Revenue), color="blue", size=3) + | |
ylim(0, NA) + | |
ylab("Aastatulu [$]") + xlab("Aasta") + | |
correct_theme | |
g_freq_good | |
ggsave("graphs/freq_good.svg", width=16, height=9) | |
# ---- Tiny example ---- | |
ggplot(data4) + | |
geom_line(aes(x=Year, y=Revenue), size=15, color="blue") + | |
ylim(as.numeric(c(NA, NA))) + | |
xlab("") + ylab("") + ggtitle("") + | |
theme(axis.line=element_blank(),axis.text.x=element_blank(), | |
axis.text.y=element_blank(),axis.ticks=element_blank(), | |
axis.title.x=element_blank(), | |
axis.title.y=element_blank(),legend.position="none", | |
panel.background=element_blank(),panel.grid.major=element_blank(), | |
panel.grid.minor=element_blank(),plot.background=element_blank(), | |
panel.border=element_rect(fill=NA, size=10)) | |
ggsave("graphs/tiny.svg", width=16, height=9, dpi=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment