### Packages
library(ggplot2)
library(dplyr)
load("./data/dow_jones_industrial.rda")
dow_jones_industrial %>% sapply(class)
date open high low close volume
"Date" "numeric" "numeric" "numeric" "numeric" "integer"
dow_jones_industrial %>% head()
### Figure 14.1
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close)) +
geom_line(linewidth = 0.75) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal()
### Figure 14.3
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close, color = "close")) +
geom_line(linewidth = 0.75) +
geom_smooth(aes(color = "smooth"), size = 1, na.rm = T, se = F) +
scale_color_manual(name = NULL,
values = c(close = "#000000", smooth = "#0072b2"),
labels = c("Close", "LOESS smoother")) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal() +
theme(legend.position = c(0.8, 0.4))
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
# span: Controls the amount of smoothing for the default loess smoother.
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close, color = "close")) +
geom_line(linewidth = 0.75) +
geom_smooth(aes(color = "smooth"), size = 1, na.rm = T, se = F,
span = 0.3) +
scale_color_manual(name = NULL,
values = c(close = "#000000", smooth = "#0072b2"),
labels = c("Close", "LOESS smoother")) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal() +
theme(legend.position = c(0.8, 0.4))
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
# gam: Generalized additive models - Cubic regression splines with 5 knots.
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close, color = "close")) +
geom_line(linewidth = 0.75) +
geom_smooth(aes(color = "smooth"), size = 1, na.rm = T, se = F,
method = "gam", formula = y ~ s(x, k = 5, bs = 'cr')) +
scale_color_manual(name = NULL,
values = c(close = "#000000", smooth = "#0072b2"),
labels = c("Close", "Cubic regression splines")) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal() +
theme(legend.position = c(0.8, 0.4))
# gam: Generalized additive models - Thin-plate regression spline with 3 knots.
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close, color = "close")) +
geom_line(linewidth = 0.75) +
geom_smooth(aes(color = "smooth"), size = 1, na.rm = T, se = F,
method = "gam", formula = y ~ s(x, k = 3)) +
scale_color_manual(name = NULL,
values = c(close = "#000000", smooth = "#0072b2"),
labels = c("Close", "Thin-plate regression spline")) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal() +
theme(legend.position = c(0.8, 0.4))
# gam: Generalized additive models - Gaussian process spline with 6 knots.
dow_jones_industrial %>%
filter(date >= "2008/12/31" & date <= "2010/01/10") %>%
ggplot(aes(date, close, color = "close")) +
geom_line(linewidth = 0.75) +
geom_smooth(aes(color = "smooth"), size = 1, na.rm = T, se = F,
method = "gam", formula = y ~ s(x, k = 6, bs = 'gp')) +
scale_color_manual(name = NULL,
values = c(close = "#000000", smooth = "#0072b2"),
labels = c("Close", "Gaussian process spline")) +
ylab("Dow Jones Industrial Average") +
scale_x_date(name = NULL,
date_labels = "%Y년 %b월") +
theme_minimal() +
theme(legend.position = c(0.8, 0.4))
### Figure 14.7
load("./data/blue_jays.rda")
ggplot(blue_jays, aes(Mass, Head, fill = KnownSex, color = KnownSex)) +
geom_point(pch = 21, color = "white", size = 3) +
geom_smooth(method = "lm", linewidth = 0.75,
se = FALSE, fullrange = TRUE) +
xlab("body mass (g)") +
ylab("head length (mm)") +
scale_fill_manual(
values = c(F = "#D55E00", M = "#0072B2"),
breaks = c("F", "M"),
labels = c("female birds ", "male birds"),
name = NULL,
guide = guide_legend(direction = "horizontal")
) +
scale_color_manual(
values = c(F = "#D55E00", M = "#0072B2"),
breaks = c("F", "M"),
labels = c("female birds ", "male birds"),
name = NULL
) +
theme_minimal() +
theme(
legend.position = "top",
legend.justification = "right"
)
`geom_smooth()` using formula = 'y ~ x'
### Figure 14.12 - data
load("./data/CO2.rda")
CO2 %>% head()
### Figure 14.12
CO2 %>%
ggplot(aes(date_dec, co2_interp)) +
geom_line(linewidth = 0.75, color = "#0072B2") +
ylab(parse(text = "`CO`[2]*` concentration (ppm)`")) +
xlab(NULL) +
theme_minimal()
# Create date variable
CO2 %>%
mutate(date = as.Date(paste(year, month, 1, sep = "-"))) %>%
ggplot(aes(date, co2_interp)) +
geom_line(linewidth = 0.75, color = "#0072B2") +
ylab(parse(text = "`CO`[2]*` concentration (ppm)`")) +
scale_x_date(name = NULL, date_labels = "%Y") +
theme_minimal()
### Figure 14.13 - data
# use complete years only
CO2_complete <- filter(CO2, year >= 1959, year < 2018)
# convert to time series object
CO2_ts <- ts(data = CO2_complete$co2_interp,
start = 1959, end = c(2017, 12), frequency = 12)
# detrend via STL method
CO2_stl <- stl(CO2_ts, s.window = 7)
CO2_detrended <- mutate(
CO2_complete,
`monthly average` = co2_interp,
`seasonal fluctuations` = CO2_stl$time.series[, "seasonal"],
`long-term trend` = CO2_stl$time.series[, "trend"],
remainder = CO2_stl$time.series[, "remainder"]
)
### Figure 14.13
facet_labels <- c("monthly average", "long-term trend",
"seasonal fluctuations", "remainder")
ggp <- CO2_detrended %>%
select(date_dec, `monthly average`:remainder) %>%
tidyr::gather("variable", "value", -date_dec) %>%
mutate(variable = factor(variable, levels = facet_labels)) %>%
filter(date_dec >= 1989) %>%
ggplot(aes(date_dec, value)) +
geom_line(linewidth = 0.75, color = "#0072B2") +
facet_wrap(facets = ~variable, ncol = 1, scales = "free") +
theme_minimal()
경고: attributes are not identical across measure variables; they will be dropped
ggp