-
Notifications
You must be signed in to change notification settings - Fork 0
/
009_ggplot_lines.R
152 lines (93 loc) · 3.7 KB
/
009_ggplot_lines.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
145
146
147
148
149
150
151
152
### Line Graphs
# geom_line()
# geom_path()
# geom_step()
library(ggplot2)
# Line graphs require a datframe with 2 columns: the x axis and the y axis
# A third column could be for groups
### Example 1.
library(gapminder)
head(gapminder)
tail(gapminder)
d1 <- c("Algeria", "Sudan", "Kenya", "Zimbabwe")
d2 <- subset(gapminder, country %in% d1) #%in% is a function which just means 'in'
head(d2)
ggplot(d2)
ggplot(d2, aes(x = year, y = lifeExp, color = country)) # tells R to color & group by 'country'
#add point and line by 'country'
ggplot(d2, aes(x = year, y = lifeExp, color = country)) + geom_line() + geom_point()
#alter line width
ggplot(d2, aes(x = year, y = lifeExp, color = country)) + geom_line(lwd=1) + geom_point()
#geom_path() is often equivalent to geom_line() - there are some differences (not here though)
ggplot(d2, aes(x = year, y = lifeExp, color = country)) + geom_path() + geom_point()
p <- ggplot(d2, aes(x = year, y = lifeExp, color = country)) + geom_line() + geom_point()
p
p + theme(legend.position = "none")
ggplot(d2, aes(x = year, y = lifeExp, color = country)) +
geom_line() +
geom_point()
## Just to give you an example of customization
# here we add text to the end points of each line
library(ggrepel)
p +
theme(legend.position = "none") +
geom_text_repel(
data = subset(d2, year==2007),
aes(label = country),
size = 4,
nudge_y = .75,
nudge_x = 1,
segment.color=NA
)
ggplot(d2, aes(x = year, y = lifeExp, color = country)) +
geom_line(lwd=1) +
geom_point() +
theme(legend.position = "none") +
theme_minimal() +
scale_color_manual(values=c("navy", "blue", "dodgerblue", "cyan")) +
xlab("Year") +
ylab("Life Expectancy") +
ggtitle("Life Expectancy Since 1950 in select African Countries",
subtitle = "source: gapminder.org")
### What if we plotted all African countries?
africa <- subset(gapminder, continent=="Africa")
zim <- subset(africa, country=="Zimbabwe")
unique(africa$country)
ggplot(africa, aes(x = year, y = lifeExp, color = country)) + geom_line()
ggplot(africa, aes(x = year, y = lifeExp, color = country)) +
geom_line() + facet_wrap(~country, ncol=8) + theme(legend.position="none")
ggplot(africa, aes(x = year, y = lifeExp,group=country)) +
geom_line(color="gray66", alpha=.25) +
geom_line(data=zim, color="firebrick", lwd=1)+
theme(legend.position="none") +
theme_classic() +
ggtitle("Zimbabwe vs Other African Countries") +
xlab("Year") +
ylab("Life Expectancy")
### Compare median value of each continent
ggplot(gapminder, aes(x = year, y = lifeExp,group=country)) +
geom_line(color="dodgerblue", alpha=.15) +
facet_wrap(~continent, ncol=5) +
theme_classic()
library(tidyverse)
medianlife <- gapminder %>% group_by(year,continent) %>% summarise(median=median(lifeExp))
medianlife
#same as above - but 'country' is specific to first set of lines
ggplot(gapminder, aes(x = year, y = lifeExp)) +
geom_line(aes(group=country), color="dodgerblue", alpha=.15) +
facet_wrap(~continent, ncol=5) +
theme_classic()
#can now add median:
ggplot(gapminder, aes(x = year, y = lifeExp)) +
geom_line(aes(group=country), color="dodgerblue", alpha=.15) +
facet_wrap(~continent, ncol=5) +
theme_classic() +
geom_line(data=medianlife, aes(x=year,y=median), color="navy", lwd=1)
### Drop Oceania
gapminder1 <- droplevels(subset(gapminder, continent != "Oceania"))
medianlife1 <- droplevels(subset(medianlife, continent != "Oceania"))
ggplot(gapminder1, aes(x = year, y = lifeExp)) +
geom_line(aes(group=country), color="dodgerblue", alpha=.15) +
facet_wrap(~continent, ncol=5) +
theme_classic() +
geom_line(data=medianlife1, aes(x=year,y=median), color="navy", lwd=1)