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

Przygotowanie

library(scales)
library(dplyr)
library(archivist)
library(gridExtra)
library(rworldmap)
library(ggthemes)
library(latticeExtra)
library(lattice)
library(ggplot2)

# store everything in the archivist repo
setLocalRepo("arepo")

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

print.ggplot <- function(x, ...) {
  hash <- saveToRepo(x)
  cat("Load: [`archivist::aread('pbiecek/Eseje/arepo/",hash,"')`](https://github.com/pbiecek/Eseje/raw/master/arepo/gallery/",hash,".rda)\n", sep="")
  ggplot2:::print.ggplot(x, ...)
}

Dane

W tym skrypcie wykorzystujemy pakiet SmarterPoland i zbiory danych

library(SmarterPoland)
head(countries)
              country birth.rate death.rate population continent
1         Afghanistan       34.1        7.7      30552      Asia
2             Albania       12.9        9.4       3173    Europe
3             Algeria       24.3        5.7      39208    Africa
4             Andorra        8.9        8.4         79    Europe
5              Angola       44.1       13.9      21472    Africa
6 Antigua and Barbuda       16.5        6.8         90  Americas
countries <- na.omit(countries)
head(maturaExam)
  id_ucznia punkty  przedmiot  rok
1         4     14 matematyka 2011
2         4     31  j. polski 2011
3         5     19 matematyka 2010
4         5     35  j. polski 2010
5         7     16 matematyka 2010
6         7     43  j. polski 2010

Warstwy

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))

# pierwszy przyklad
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/24ea7c04b861083d4bf56eee1c5a17b7')