Note: All of the following graphs were created using ggplot2 and are inspired from the book Storytelling With Data: Let’s Practice!
(Ref: Knaflic, Cole. Storytelling With Data: Let’s Practice! Wiley, © 2019.)
The author created all the figures using Excel and PowerPoint.
In this post, I will use mainly two ubiquitous packages, namely ggplot2 (for data visualization) and tidyverse (for data transforming).
library(ggplot2)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble 3.0.5 v dplyr 1.0.3
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.0
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(stringr)
library(ggtext)
## Warning: package 'ggtext' was built under R version 4.0.5
year <- c(2005:2019)
total <- c(8.7,8.3,8.6,8.9,8.4,8.6,7.5,7.2,6.9,7.4,6.6,8.0,7.3,7.0,6.3)
organic <- c(3.3,3.5,3.7, 3.6, 3.4, 3.1, 3.2,3.5, 3.2, 3.8, 3.5, 4.5, 4.1, 4.2,3.8)
referral <- c(5.4,4.8,4.9,5.3,5.0,5.5,4.3,3.7,3.7,3.6,3.1,3.5,3.2,2.8,2.5)
df_wide <- tibble(year, organic, referral, total)
df_wide
## # A tibble: 15 x 4
## year organic referral total
## <int> <dbl> <dbl> <dbl>
## 1 2005 3.3 5.4 8.7
## 2 2006 3.5 4.8 8.3
## 3 2007 3.7 4.9 8.6
## 4 2008 3.6 5.3 8.9
## 5 2009 3.4 5 8.4
## 6 2010 3.1 5.5 8.6
## 7 2011 3.2 4.3 7.5
## 8 2012 3.5 3.7 7.2
## 9 2013 3.2 3.7 6.9
## 10 2014 3.8 3.6 7.4
## 11 2015 3.5 3.1 6.6
## 12 2016 4.5 3.5 8
## 13 2017 4.1 3.2 7.3
## 14 2018 4.2 2.8 7
## 15 2019 3.8 2.5 6.3
df_long <- df_wide %>%
pivot_longer(cols = c("organic","referral","total"),
names_to = "type",
values_to = "value")
head(df_long,10)
## # A tibble: 10 x 3
## year type value
## <int> <chr> <dbl>
## 1 2005 organic 3.3
## 2 2005 referral 5.4
## 3 2005 total 8.7
## 4 2006 organic 3.5
## 5 2006 referral 4.8
## 6 2006 total 8.3
## 7 2007 organic 3.7
## 8 2007 referral 4.9
## 9 2007 total 8.6
## 10 2008 organic 3.6
theme_attention <- theme(plot.title = element_text(size = 20, vjust = 6, hjust = -0.15),
axis.text.x = element_text(size=12, color = "#6d6d6d"),
axis.text.y = element_text(size=12, color = "#6d6d6d",hjust = 0.1),
axis.title.x= element_text(size = 12, hjust = 0, vjust = -4, color = "#6d6d6d"),
axis.title.y = element_text(size = 12, hjust = 1 ,vjust = 4, color = "#6d6d6d"),
axis.line.x= element_line(color="#a9a9a9"),
axis.line.y= element_line(color="#a9a9a9", size = 1),
axis.ticks = element_line(color="#a9a9a9") ,
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(1,2.75,1,1),"cm"))
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#6d6d6d", size = 1.5) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#6d6d6d", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#6d6d6d", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#6d6d6d", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Conversion rate over time",
y = "CONVERSION RATE (%)",
x = "FISCAL YEAR") +
theme_attention
ggsave("Figure 4.3a.png", width = 9, height = 6, units = "in", dpi = 300)
Assumption : we would like to draw our audience’s attention to the Referral line.
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 1.) +
geom_line(data = filter(df_long, type == "referral") , color = "#a9a9a9", size = 2) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#6d6d6d", hjust = 0, size = 4.5, fontface="bold") +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3f Thicken the line.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(data = filter(df_long, type == "referral"), color = "#a9a9a9", size = 1.5, linetype = "dashed") +
geom_line(data = filter(df_long, type == "organic" | type == "total"), color = "#a9a9a9", size = 1.5) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3g Change the line style.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(data = filter(df_long, type == "referral"), color = "black", size = 1.5) +
geom_line(data = filter(df_long, type == "organic" | type == "total"), color = "#a9a9a9", size = 1.5) +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="black", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3h Make it darker.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(data = filter(df_long, type == "organic" | type == "total"), color = "#a9a9a9", size = 1.5) +
geom_line(data = filter(df_long, type == "referral"), color = "black", size = 1.5) + # putting the line in front
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="black", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3i Position in front of other data.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(data = filter(df_long, type == "organic" | type == "total"), color = "#a9a9a9", size = 1.5) +
geom_line(data = filter(df_long, type == "referral"), color = "#FF0000", size = 1.5) + # putting the line in front
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color = "#FF0000", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5) +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3j Change the hue.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 2) +
# geom_line(data = filter(df_long, type == "referral") , color = "#a9a9a9", size = 2) +
annotate("text", x = 2019.2, y = 3.8, hjust = 0,
label = "ORGANIC", color = "#a9a9a9", size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 2.5, hjust = 0,
label = "REFERRAL", color ="#a9a9a9", size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 6.3, hjust = 0,
label = "TOTAL", color = "#a9a9a9", size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Conversion rate over time: Referral decreasing markedly since 2010",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme(plot.title = element_text(size = 20, vjust = 6, hjust = 0.5),
axis.text.x = element_text(size=12, color = "#a9a9a9"),
axis.text.y = element_text(size=12, color = "#a9a9a9",hjust = 0.1),
axis.title.x= element_text(size = 12, hjust = 0, vjust = -4, color = "#a9a9a9"),
axis.title.y = element_text(size = 12, hjust = 1 ,vjust = 4, color = "#a9a9a9"),
axis.line.x= element_line(color="#a9a9a9"),
axis.line.y= element_line(color="#a9a9a9", size = 1),
axis.ticks = element_line(color="#a9a9a9"),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(1,2.75,1,1),"cm"))
ggsave("Figure 4.3k Use title words to prime audience.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data = filter(df_long, type == "referral"),
aes(x= year, y = value, group = type))+
# geom_line(color = "#a9a9a9", size = 2) +
geom_line(color = "#a9a9a9", size = 2) +
#annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
#annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3l Eliminate the other data.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 1.75) +
geom_point(data = filter(df_long, type == "referral") , color = "#a9a9a9", size = 4.25) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 10) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3m Add data markers.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 2) +
geom_point(data = filter(df_long, type == "referral") , color = "#a9a9a9", size = 4) +
geom_text(data = filter(df_long, type == "referral"),
aes(label = paste(format(value), "%", sep = "")),
size = 4.5, color = "#a9a9a9", vjust = -1) +
annotate("text", x = 2019.2, y = 3.8, label = "ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 2.5, label = "REFERRAL", color ="#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 6.3, label = "TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 5) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention
ggsave("Figure 4.3n Add data labels.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 2) +
annotate("text", x = 2019.2, y = 3.8, label = "3.8% ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 2.5, label = "2.5% REFERRAL", color ="#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.2, y = 6.3, label = "6.3% TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 5) +
labs(title = "Coversion rate over time",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme_attention +
theme(plot.margin = unit(c(1,4,1,1),"cm"))
ggsave("Figure 4.3o Employ end markers and labels.png", width = 9, height = 6, units = "in", dpi = 300)
ggplot(data= df_long,
aes(x= year, y = value, group = type))+
geom_line(color = "#a9a9a9", size = 2) +
geom_line(data = filter(df_long, type == "referral"), color = "#FF0000", size = 1.5) + # putting the line in front
geom_point(data = filter(df_long, type == "referral", year %in% c(2010, 2016, 2019)), color = "#FF0000", size = 5) +
annotate("text", x = 2019.3, y = 3.8, label = "3.8% ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.3, y = 2.5, label = "2.5% REFERRAL", color = "#FF0000", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.3, y = 6.3, label = "6.3% TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 5) +
labs(title = "Coversion rate over time: <span style = 'color:#FF0000;'>**referral decreasing markedly since 2010**</span>",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme(plot.title = element_markdown(size = 18, hjust = 0.625),
axis.text.x = element_text(size=12, color = "#a9a9a9"),
axis.text.y = element_text(size=12, color = "#a9a9a9",hjust = 0.1),
axis.title.x= element_text(size = 12, hjust = 0, vjust = -4, color = "#a9a9a9"),
axis.title.y = element_text(size = 12, hjust = 1 ,vjust = 4, color = "#a9a9a9"),
axis.line.x= element_line(color="#a9a9a9"),
axis.line.y= element_line(color="#a9a9a9", size = 1),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0.5,4,1,1),"cm"))
df1 <- tibble(
label = "<span style='color:#FF0000'>**2010: all-time referral conversion high**</span>
<span style='color:#000000'>(5.5%). Strong partnerships historically
meant steady conversions. Entry of competitor ABC has markedly impacted
referral quality: fewer are buying</span>",
x = 2010,
y = 2.7)
df2 <- tibble(
label = "<span style='color:#FF0000'>**2016: new campaigns**</span>
<span style='color:#000000'>led to brief uptick; steady decrease since then</span>",
x = 2016,
y = 2)
ggplot(data= df_long, aes(x = year, y = value)) +
geom_line(data = filter(df_long, type == "organic"), color = "#a9a9a9", size = 1.5) +
geom_line(data = filter(df_long, type == "total"), color = "#a9a9a9", size = 1.5) +
geom_line(data = filter(df_long, type == "referral"), color = "#FF0000", size = 1.5) +
geom_point(data = filter(df_long, type == "referral", year %in% c(2010, 2016, 2019)), color = "#FF0000", size = 5) +
geom_textbox(data = df1, aes(x,y, label = label), box.color = "white", fill = "white", hjust = 0, vjust = 0.925,
# box.r = unit(10, "pt"),
width = unit(200, "pt"),
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
geom_textbox(data = df2, aes(x,y, label = label), box.color = "white", fill = "white", hjust = 0, vjust = 1,
box.padding = unit(c(0, 0, 0, 0), "pt"),) +
annotate("text", x = 2019.3, y = 3.8, label = "3.8% ORGANIC", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.3, y = 2.5, label = "2.5% REFERRAL", color = "#FF0000", hjust = 0, size = 4.5, fontface = "bold") +
annotate("text", x = 2019.3, y = 6.3, label = "6.3% TOTAL", color = "#a9a9a9", hjust = 0, size = 4.5, fontface = "bold") +
annotate("segment", x = 2010,xend= 2010, y = 2.9, yend = 5.2,colour = "#a9a9a9", size = 0.85) +
annotate("segment", x = 2016, xend= 2016,y = 2.1, yend = 3.2,colour = "#a9a9a9", size = 0.85) +
scale_x_continuous(expand = c(0, 0),n.breaks = 14) +
coord_cartesian(xlim = c(2005, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(0, 10), n.breaks = 5) +
labs(title = "Coversion rate over time: <span style = 'color:#FF0000;'>**referral decreasing markedly since 2010**</span>",
x = "FISCAL YEAR",
y = "CONVERSION RATE (%)") +
theme(plot.title = element_markdown(size = 18, hjust = 0.625),
axis.text.x = element_markdown(size=12, color = "#a9a9a9"),
axis.text.y = element_markdown(size=12, color = "#a9a9a9",hjust = 0.1),
axis.title.x= element_markdown(size = 12, hjust = 0, vjust = -4, color = "#a9a9a9"),
axis.title.y = element_markdown(size = 12, hjust = 1 ,vjust = 4, color = "#a9a9a9"),
axis.line.x= element_line(color="#a9a9a9"),
axis.line.y= element_line(color="#a9a9a9", size = 1),
axis.ticks = element_line(color="#a9a9a9"),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(1,4,1,1),"cm"))
ggsave("Figure 4.3p Combine multiple preattentive attributes.png", width = 9, height = 6, units = "in", dpi = 300)
the scenario: We work at Financial Savings and want to compare your bank’s performance against your peers’. We have data on bank index (branch satisfaction) over time for your bank plus a number of your competitors.
data_w <- read_csv("bank.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## bank = col_character(),
## `2012` = col_double(),
## `2013` = col_double(),
## `2014` = col_double(),
## `2015` = col_double(),
## `2016` = col_double(),
## `2017` = col_double(),
## `2018` = col_double(),
## `2019` = col_double()
## )
tail(data_w, 10)
## # A tibble: 10 x 9
## bank `2012` `2013` `2014` `2015` `2016` `2017` `2018` `2019`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Bank 15 784 798 798 790 830 828 818 852
## 2 Bank 16 818 811 824 815 848 837 833 851
## 3 Bank 17 795 786 794 803 817 817 819 856
## 4 Bank 18 823 829 811 831 837 850 827 866
## 5 Bank 19 775 775 769 783 806 813 800 832
## 6 Bank 20 757 771 789 804 813 822 809 852
## 7 Bank 21 767 759 757 704 751 768 771 811
## 8 Bank 22 772 761 755 758 791 792 787 826
## 9 Industry Average 795 800 804 812 830 832 824 846
## 10 Financial Savings 774 785 805 833 839 843 827 836
data_l <- data_w%>%
pivot_longer(cols = c("2012","2013","2014","2015","2016","2017","2017","2018","2019"),
names_to = "year",
values_to = "score")
str(data_l)
## tibble [192 x 3] (S3: tbl_df/tbl/data.frame)
## $ bank : chr [1:192] "Bank 1" "Bank 1" "Bank 1" "Bank 1" ...
## $ year : chr [1:192] "2012" "2013" "2014" "2015" ...
## $ score: num [1:192] 825 846 846 847 865 862 850 868 810 829 ...
data_l$year <- as.numeric(data_l$year)
ggplot(data = data_l,
aes(x= year, y = score, group=bank))+
geom_line(size = 1.5, color = "#a9a9a9") +
geom_line(data = filter(data_l, bank == "Industry Average"), size = 1.5, color = "black") +
geom_line(data = filter(data_l, bank == "Financial Savings"), size = 1.5, color = "#4169e1") +
geom_point(data = filter(data_l, bank == "Industry Average", year == 2019), size = 4, color = "black") +
geom_point(data = filter(data_l, bank == "Financial Savings", year == 2019), size = 4, color = "#4169e1") +
annotate("text", x = 2020, y = 850, label = "Industry Average", color = "black", size = 5) +
annotate("text", x = 2020, y = 837.5, label = "Financial Savings", color = "#4169e1", size = 5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 10) +
coord_cartesian(xlim = c(2012, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(700, 900), n.breaks = 10) +
labs(title = "BRANCH SATISFACTION<br><span style = 'color:#4169e1;'>**Financial Savings**</span> below <span style ='color:#000000;'>**industry**</span> for the first 5 years") +
ylab("SATISFACTION SCORE") +
xlab("SURVEY YEAR") +
theme(plot.title = element_markdown(size = 20, lineheight = 1.25),
# plot.subtitle = element_markdown(size = 20, vjust = 1, hjust = 0.5),
plot.title.position = "plot", # applied for subtitle
# plot.subtitle = element_markdown(size=16, hjust = 1), # when using ggtext package
# Adjust Space Between ggplot2 Axis Labels and Plot Area
axis.text.x = element_text(size=12, vjust = -2) ,
axis.text.y = element_text(size=12, hjust = -3), # ca marche pas cet commande
axis.title.x= element_text(size = 12, hjust = 0, vjust = -2.5, color ="black"), # change the position of label on y axis
axis.title.y = element_text(size = 12, hjust = 1, vjust = 2.5 , color ="black"),# change the position of label on x axis
# axis.title.x=element_blank(),
axis.line.x= element_line(color="grey"),
axis.line.y= element_line(color="grey", size = 1),
axis.ticks = element_line(color="#a9a9a9"),
# axis.text.y = element_blank(),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin=unit(c(0.5,4.5,1,1),"cm")) # to change the margin top, right. bottom. left # work with coord_cartesian()
ggsave("Figure 4.4b.png", width = 9, height = 6, units = "in", dpi = 300)
df <- tibble(label =
"<span style='color:#4169e1'>**Financial Savings increased 1%**</span>
in satisfaction score in the past year, whereas
<span style='color:#000000'>**the industry increased an average of 3%**</span>
<span style ='color:#93C572'>(1-5% each)</span>",
x = 2018, y = 760, color ="white", contourfill = "white")
ggplot(data = data_l,
aes(x= year, y = score))+ # can not use GROUP her when using geom_textbox
geom_line(aes(group = bank), size = 1.25, color = "#a9a9a9") + # have to use GROUP here
geom_line(data = filter(data_l, year == 2018 | year == 2019), aes(group = bank), size = 1.25, color = "#93C572") +
geom_line(data = filter(data_l, bank == "Industry Average"), size = 1.5, color = "black") +
geom_line(data = filter(data_l, bank == "Financial Savings"),size = 1.5, color = "#4169e1") +
geom_point(data = filter(data_l, bank == "Industry Average", year == 2019), size = 4, color = "black") +
geom_point(data = filter(data_l, bank == "Financial Savings", year == 2019), size = 4, color = "#4169e1") +
geom_textbox(data = df, aes(x,y, label = label),size= 4.5, box.color = "white",
fill = "white", hjust = 0, vjust = 0.9,
halign = 1, # alignement for the text inside the box
# box.r = unit(10, "pt"),
width = unit(160, "pt"),
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
annotate("text", x = 2020, y = 850, label = "Industry Average", color = "black", size = 5) +
annotate("text", x = 2020, y = 837.5, label = "Financial Savings", color = "#4169e1", size = 5) +
scale_x_continuous(expand = c(0, 0),n.breaks = 10) +
coord_cartesian(xlim = c(2012, 2019), clip = 'off') +
scale_y_continuous(expand = c(0, 0),limits = c(700, 900), n.breaks = 10) +
labs(title = "BRANCH SATISFACTION<br><span style = 'color:#4169e1;'>**Financial Savings**</span> below <span style ='color:#000000;'>**industry**</span> for the first 5 years") +
ylab("SATISFACTION SCORE") +
xlab("SURVEY YEAR") +
theme(plot.title = element_markdown(size = 18, lineheight = 1.25),
# plot.subtitle = element_markdown(size = 20, vjust = 1, hjust = 0.5),
plot.title.position = "plot", # applied for subtitle
# plot.subtitle = element_markdown(size=16, hjust = 1), # when using ggtext package
# Adjust Space Between ggplot2 Axis Labels and Plot Area
axis.text.x = element_text(size=12, vjust = -2) ,
axis.text.y = element_text(size=12, hjust = -3), # ca marche pas cet commande
axis.title.x= element_text(size = 12, hjust = 0, vjust = -2.5, color ="black"), # change the position of label on y axis
axis.title.y = element_text(size = 12, hjust = 1, vjust = 2.5 , color ="black"),# change the position of label on x axis
# axis.title.x=element_blank(),
axis.line.x= element_line(color="grey"),
axis.line.y= element_line(color="grey", size = 1),
axis.ticks = element_line(color="#a9a9a9"),
# axis.text.y = element_blank(),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin=unit(c(0.5,4.5,1,1),"cm")) # to change the margin top, right. bottom. left # work with coord_cartesian()
ggsave("Fig4.4c Focus on latest year-to-year period of time.png", width = 9, height = 6, units = "in",dpi = 300)