-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture7.R
145 lines (114 loc) · 3.56 KB
/
lecture7.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
library(usethis)
library(devtools)
#install.packages("gapminder")
#install.packages("ggthemes")
#install.packages("Hmisc")
#install.packages("wesanderson")
#install.packages("ggridges")
library(pacman)
library(tidyverse)
# install_github("andrew-griffen/gdata")
# install_github("andrew-griffen/griffen")
library(griffen)
library(gdata)
p_load(gapminder,ggthemes,Hmisc,wesanderson,ggridges)
p <- ggplot(
data <- gapminder,
mapping = aes(x = gdpPercap, y = lifeExp, color = continent)
) +
geom_point() + # check https://www.color-hex.com/ to get the color code
labs(title = "Life expectancy vs. GDP",
x = "GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = wes_palette("Royal2"))
print(p)
wes_palette("Royal2")
unclass(wes_palette("Royal2"))
#######
p <- ggplot(
data <- gapminder,
mapping = aes(x = gdpPercap, y = lifeExp, color = country)
) +
geom_point(show.legend = FALSE) +
labs(title = "Life expectancy vs. GDP",
x = "GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = country_colors)
print(p)
####### Log
p <- ggplot(
data <- gapminder,
mapping = aes(x =log(gdpPercap), y = lifeExp, color = country)
) +
geom_point(show.legend = FALSE) +
labs(title = "Life expectancy vs. Log GDP",
subtitle = "Hans Rosling's Gapminder Data",
caption = "Note: Year from 1957-2007",
x = "Log GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = country_colors)
print(p)
######## Theme
library(hrbrthemes)
theme_set(theme_economist(base_size = 18))
theme_set(theme_excel(base_size = 18))
theme_set(theme_wsj(base_size = 18))
theme_set(theme_tufte(base_size = 18))
theme_set(theme_stata(base_size = 18))
theme_set(theme_ipsum(
base_size = 18,
axis_title_size =12))
######## Transparency
p <- ggplot(
data <- gapminder,
mapping = aes(x =log(gdpPercap), y = lifeExp, color = country, size = pop)
) +
geom_point(show.legend = FALSE, alpha = .7) +
labs(title = "Life expectancy vs. Log GDP",
subtitle = "Hans Rosling's Gapminder Data",
caption = "Note: Year from 1957-2007",
x = "Log GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = country_colors)
print(p)
####### Faceting
p <- ggplot(
data <- gapminder,
mapping = aes(x =log(gdpPercap), y = lifeExp, color = country, size = pop)
) +
geom_point(show.legend = FALSE, alpha = .7) +
labs(title = "Life expectancy vs. Log GDP",
subtitle = "Hans Rosling's Gapminder Data",
caption = "Note: Year from 1957-2007",
x = "Log GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = country_colors) +
facet_wrap(. ~ continent)
print(p)
####### add model
p <- ggplot(
data <- gapminder,
mapping = aes(x =log(gdpPercap), y = lifeExp, color = country)
) +
geom_point(aes(size = pop), show.legend = FALSE) +
labs(title = "Life expectancy vs. Log GDP",
subtitle = "Hans Rosling's Gapminder Data",
caption = "Note: Year from 1957-2007",
x = "Log GDP per capital",
y = "Life expectancy") +
scale_color_manual(values = country_colors) +
facet_wrap(. ~ continent) +
geom_smooth(aes(x = log(gdpPercap),
y = lifeExp,
group = continent),
formula = y ~ x,
method = 'loess',
color = "black",
se = FALSE,
show.legend = FALSE)
print(p)
#####
p <- ggplot(data = gdata$codes,
mapping = aes(x = pgg_contribution_round1, y = pgg_contribution_round2)) +
geom_point()
print(p)