forked from jennybc/ggplot2-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapminder-ggplot2-stripplot.r
52 lines (40 loc) · 1.79 KB
/
gapminder-ggplot2-stripplot.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' ---
#' author: "Jenny Bryan"
#' output:
#' html_document:
#' keep_md: TRUE
#' ---
#+ setup, include = FALSE
library(knitr)
opts_chunk$set(fig.path = 'figure/stripplot-', error = TRUE)
#' Note: this HTML is made by applying `knitr::spin()` to an R script. So the
#' narrative is very minimal.
library(ggplot2)
#' pick a way to load the data
#gdURL <- "http://tiny.cc/gapminder"
#gapminder <- read.delim(file = gdURL)
#gapminder <- read.delim("gapminderDataFiveYear.tsv")
library(gapminder)
str(gapminder)
#' stripplots: univariate scatterplots (but w/ ways to also convey 1+ factors)
ggplot(gapminder, aes(x = continent, y = lifeExp)) + geom_point()
#' we have an overplotting problem; need to spread things out
ggplot(gapminder, aes(x = continent, y = lifeExp)) + geom_jitter()
#' we can have less jitter in x, no jitter in y, more alpha transparency
ggplot(gapminder, aes(x = continent, y = lifeExp)) +
geom_jitter(position = position_jitter(width = 0.1, height = 0), alpha = 1/4)
#' boxplots -- covered properly elsewhere
ggplot(gapminder, aes(x = continent, y = lifeExp)) + geom_boxplot()
#' raw data AND boxplots
ggplot(gapminder, aes(x = continent, y = lifeExp)) +
geom_boxplot(outlier.colour = "hotpink") +
geom_jitter(position = position_jitter(width = 0.1, height = 0), alpha = 1/4)
#' superpose a statistical summary
ggplot(gapminder, aes(x = continent, y = lifeExp)) +
geom_jitter(position = position_jitter(width = 0.1), alpha = 1/4) +
stat_summary(fun.y = median, colour = "red", geom = "point", size = 5)
#' let's reorder the continent factor based on lifeExp
ggplot(gapminder, aes(reorder(x = continent, lifeExp), y = lifeExp)) +
geom_jitter(position = position_jitter(width = 0.1), alpha = 1/4) +
stat_summary(fun.y = median, colour = "red", geom = "point", size = 5)
sessionInfo()