-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot2 ribbon.R
51 lines (42 loc) · 1.49 KB
/
ggplot2 ribbon.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
## ggplot2: create ribbon band
library(ggplot2)
# Bangkok: Annual Weather Averages (as of 2016-03-07)
browseURL("http://www.holiday-weather.com/bangkok/averages/")
m <- factor(month.abb, levels = month.abb)
m
d <-
data.frame(
month = m,
avg = c(27, 28, 30, 31, 30, 30, 30, 30, 29, 29, 28, 26),
hi = c(32, 33, 34, 35, 34, 33, 33, 33, 32, 32, 31, 31),
lo = c(21, 23, 25, 26, 26, 26, 26, 26, 25, 25, 24, 21)
)
View(d)
# create ggplot object ----
ggplot(d, aes(month, avg)) + geom_line()
ggplot(d, aes(month, avg, group = 1)) + geom_line()
ggplot(d, aes(month, avg, group = 1)) +
ylim(0, max(d$avg)) +
geom_line()
# create basic ggplot object
g <- ggplot(d, aes(month, avg, group = 1)) +
ylim(0, max(d$hi))
summary(g)
# layer concept: geom_ribbon before geom_line ----
g + geom_ribbon(aes(ymin = lo, ymax = hi)) + geom_line()
g + geom_line() + geom_ribbon(aes(ymin = lo, ymax = hi))
# set ribbon band color and alpha level ----
g + geom_ribbon(aes(ymin = lo, ymax = hi), alpha = .3, fill = "yellow") +
geom_line(color = "blue")
# decorate: add labels, data label, points (size|color|fill) ----
g + geom_ribbon(aes(ymin = lo, ymax = hi), alpha = .3, fill = "lightblue") +
geom_line() + geom_point(
shape = 22,
fill = "green",
color = "green",
size = 4
) +
geom_text(aes(label = avg), vjust = -1) +
labs(title = "Bangkok Average Temperature (in Celsius)",
x = "", y = "temperature (Celsius)") +
theme_bw()