forked from dmi3kno/R-tidyverse-20180215
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
126 lines (90 loc) · 3.37 KB
/
script.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#' ---
#' title: "R tidyverse workshop"
#' author: "Carpentry UiO"
#' date: "`r format(Sys.Date())`"
#' output: github_document
#' ---
#' *Read more about this type of document in
#' [Chapter 20 of "Happy Git with R"](http://happygitwithr.com/r-test-drive.html)*
#'
#' Uncomment the following lines to install necessary packages
#install.packages("tidyverse")
#install.packages("maps")
#install.packages("gapminder")
#' First we need to load libraries installed previously
library(tidyverse)
#' We will source `gapminder` dataset into the session and assign it
#' to the variable with the same name
gapminder <- gapminder::gapminder
gapminder
#' Let's make our first plot
ggplot(gapminder)+
geom_point(mapping = aes(x=gdpPercap, y=lifeExp))
#' Generally speaking ggplot2 syntax follows the template:
# ggplot(<DATA>) +
# geom_<GEOM_FUNCTION>(mapping=aes(<AESTETICS>))
#' Let's learn some more about `ggplot2` and its functions
## Imagine the rest of the script from before lunch!
#'
#'
ggplot(gapminder)+
geom_jitter(mapping = aes(x=gdpPercap, y=lifeExp,
color=continent))
#' Transform gdpPercap to log scale by wrapping the name of the column into the appropriate function
ggplot(gapminder)+
geom_point(mapping = aes(x=log(gdpPercap),
y=lifeExp, color=continent))
#' There are many more aestetics in ggplot
#' See cheatsheet https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
#' Or the newer version in Rstudio. Click Help > Cheatsheets > ...
#' Introducing `size` aestetic
ggplot(gapminder)+
geom_point(mapping = aes(x=log(gdpPercap),
y=lifeExp, color=continent,
size=pop))
#' If you want to color all points in same color, provide aestetic properties
#' outside of `aes()` function
#'
ggplot(gapminder)+
geom_point(mapping = aes(x=gdpPercap, y=lifeExp),
color="blue", alpha=0.1)
#' Introducing new geom function LINES
ggplot(gapminder, mapping = aes(x=continent, y=lifeExp,
color=continent))+
geom_jitter() +
geom_boxplot()
#' More examples of multiple layers
ggplot(gapminder, mapping = aes(x=year, group=year,
y=gdpPercap))+
geom_boxplot()+
scale_y_log10()
#' We can make a histogram
ggplot(gapminder)+
geom_histogram(mapping=aes(x=gdpPercap),
bins=20)+
scale_x_log10()
#' We can try 2D density function to identify clusters
ggplot(gapminder)+
geom_density2d(mapping=aes(x=gdpPercap,
y=lifeExp))+
scale_x_log10()
#' Faceting allows you to split the data into "small multiples"
ggplot(gapminder)+
geom_point(mapping = aes(x=gdpPercap, y=lifeExp))+
facet_wrap(~year)
#' Lets label the data
ggplot(gapminder)+
geom_point(mapping=aes(x=gdpPercap,
y=lifeExp,
color=continent,
size=pop))+
scale_x_log10() +
labs(x="GDP per capita in '000 USD",
y="Life expectancy at birth, years",
color="Continent",
size="Population",
title="GDP per capita vs. Life Expectacy",
subtitle="The more money you have the longer you live",
caption="Source: GAPMINDER.ORG foundation")
#' saving plots is easy with `ggsave()` which by default saves the last plot into the filename you specify
ggsave("my_plot.png")