::p_load(ggHoriPlot,
pacman
ggthemes, tidyverse)
In-class Exercise 6: Horizon Graph
1 Overview
Building horizon plot with ggHoriPlot!
Step 1: Getting Started
For the purpose of this hands-on exercise, Average Retail Prices Of Selected Consumer Items will be used.
<- read_csv("data/AVERP.csv") %>%
averp mutate(`Date` = dmy(`Date`))
Step 2: Summary Statistics
Run summary statistics to determine origin
and horizonscale
.
summary(averp$Values)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.330 2.230 3.910 8.726 12.550 68.200
Step 3: Plotting the Horizon Graph
Show the code
#| fig-width: 12
#| fig-height: 10
%>%
averp filter(Date >= "2018-01-01") %>%
ggplot() +
geom_horizon(aes(x = Date, y=Values),
origin = "midpoint",
horizonscale = 6,
show.legend = TRUE)+
facet_grid(`Consumer Items`~.) + #<< Each row is a facet by Consumer Item
theme_few() +
scale_fill_hcl(palette = 'RdBu') +
theme(panel.spacing.y=unit(0, "lines"), strip.text.y = element_text(
size = 5, angle = 0, hjust = 0),
legend.position = 'Bottom',
axis.text.y = element_blank(),
axis.text.x = element_text(size=7),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
plot.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
panel.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
legend.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
strip.background =element_rect(fill="#f5f5f5")
+
) scale_x_date(expand=c(0,0), date_breaks = "3 month", date_labels = "%b%y") +
ggtitle('Average Retail Prices of Selected Consumer Items (Jan 2018 to Dec 2022)')
Show the code
#| fig-width: 12
#| fig-height: 10
%>%
averp filter(Date >= "2018-01-01") %>%
ggplot() +
geom_horizon(aes(x = Date, y=Values),
origin = "min",
horizonscale = 6,
show.legend = TRUE)+
facet_grid(`Consumer Items`~.) + #<< Each row is a facet by Consumer Item
theme_few() +
scale_fill_hcl(palette = 'RdBu') +
theme(panel.spacing.y=unit(0, "lines"), strip.text.y = element_text(
size = 5, angle = 0, hjust = 0),
legend.position = 'Bottom',
axis.text.y = element_blank(),
axis.text.x = element_text(size=7),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
plot.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
panel.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
legend.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
strip.background =element_rect(fill="#f5f5f5")
+
) scale_x_date(expand=c(0,0), date_breaks = "3 month", date_labels = "%b%y") +
ggtitle('Average Retail Prices of Selected Consumer Items (Jan 2018 to Dec 2022)')
%>%
averp filter(Date >= "2018-01-01") %>%
ggplot() +
geom_horizon(aes(x = Date, y=Values),
origin = "midpoint",
horizonscale = 10,
show.legend = TRUE)+
facet_grid(`Consumer Items`~.) + #<< Each row is a facet by Consumer Item
theme_few() +
scale_fill_hcl(palette = 'RdBu') +
theme(panel.spacing.y=unit(0, "lines"), strip.text.y = element_text(
size = 5, angle = 0, hjust = 0),
legend.position = 'Bottom',
axis.text.y = element_blank(),
axis.text.x = element_text(size=7),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
plot.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
panel.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
legend.background = element_rect(fill="#f5f5f5",colour="#f5f5f5"),
strip.background =element_rect(fill="#f5f5f5")
+
) scale_x_date(expand=c(0,0), date_breaks = "3 month", date_labels = "%b%y") +
ggtitle('Average Retail Prices of Selected Consumer Items (Jan 2018 to Dec 2022)')