Data preparation

library(archivist)
library(knitr)
library(ggplot2)

# store everything in the archivist repo
addHooksToPrint(class=c("ggplot", "knitr_kable"),
                 repoDir = "arepo", 
                 repo = "Eseje", user = "pbiecek", subdir = "arepo")

library(scales)
library(dplyr)
library(tidyr)
library(gridExtra)
library(rworldmap)
library(ggthemes)
library(latticeExtra)
library(lattice)

q3 <- function(x) {
  a <- quantile(x, c(0.25,0.5,0.75))
  names(a) <- c("ymin", "y", "ymax")
  a
}

opts_chunk$set(comment=NA, fig.width=6, fig.height=6, results='asis', warning=FALSE, message=FALSE, cache=FALSE)

Data

Here we are using the SmarterPoland package and two datasets.

library(SmarterPoland)
print(kable(head(countries)))

Load: archivist::aread('pbiecek/Eseje/arepo/a0f2357b814a76a46f2a42e831ea5296')

country birth.rate death.rate population continent
Afghanistan 34.1 7.7 30552 Asia
Albania 12.9 9.4 3173 Europe
Algeria 24.3 5.7 39208 Africa
Andorra 8.9 8.4 79 Europe
Angola 44.1 13.9 21472 Africa
Antigua and Barbuda 16.5 6.8 90 Americas
print(kable(head(maturaExam)))

Load: archivist::aread('pbiecek/Eseje/arepo/9797e887eb6f35519246f51946388462')

id_ucznia punkty przedmiot rok
4 14 matematyka 2011
4 31 j. polski 2011
5 19 matematyka 2010
5 35 j. polski 2010
7 16 matematyka 2010
7 43 j. polski 2010

Layers

countriesMin <- countries %>% 
  group_by(continent) %>% 
  filter(birth.rate == min(birth.rate, na.rm=TRUE))
countriesMax <- countries %>% 
  group_by(continent) %>% 
  filter(birth.rate == max(birth.rate, na.rm=TRUE))
  
theme_ggplain <- theme_bw() + theme(panel.grid.major.x = element_line(color="white"), axis.ticks=element_line(size=0), axis.text=element_text(size=0))

# foirst example
countries$continent <- reorder(countries$continent, countries$birth.rate, median, na.rm=TRUE)

ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
  geom_violin(scale="width", aes(fill=continent), color="white", alpha=0.4) + 
  stat_summary(fun.data = "q3", geom = "crossbar",
               colour = "red", width = 0.5) + 
  geom_jitter(aes(size=(population)^0.9),position=position_jitter(width = .45, height = 0),
             shape=15) +
  geom_rug(sides = "l") + 
  geom_text(data=countriesMin, vjust=2, color="blue3") + 
  geom_text(data=countriesMax, vjust=-1, color="blue3") + 
  theme_bw() + xlab("") + theme(legend.position="none", panel.grid.major.x = element_line(color="white"))

Load: archivist::aread('pbiecek/Eseje/arepo/65e430c4180e97a704249a56be4a7b88')