With color code and two different y-axis scales.
If your data is “wide” (each row might contain multiple
observations taken at the same time),
we need it to be made “tall” (each row is exactly one
data-point).
If you already have tall data, just skip the
pivot_longer().
wide_data <- data.frame(year=2019:2021,
overall=c(28.8,19.31,17.1),
deu=c(5.51, 4.16, 2.23),
gbr=c(5.51, 2.65, 3.56),
esp=c(2.35, 1.34, 1.58))
tall_data <- wide_data %>%
pivot_longer(!year, names_to="market", values_to="revenue") %>%
mutate(market=factor(market,
levels=c("deu","gbr","esp","overall")))
scale1_data <- tall_data %>% filter(market != "overall") # data points that would use the 1st scale
scale2_data <- tall_data %>% filter(market == "overall")
head(scale1_data)
## # A tibble: 6 × 3
## year market revenue
## <int> <fct> <dbl>
## 1 2019 deu 5.51
## 2 2019 gbr 5.51
## 3 2019 esp 2.35
## 4 2020 deu 4.16
## 5 2020 gbr 2.65
## 6 2020 esp 1.34
Remember to adjust the color manual for visually impaired audience!
legend_title <- "Country/Region"
# the names used in the data.frame
breaks <- c("deu", "gbr", "esp", "overall")
# the names that appear in the legend
labels <- c("Germany", "the UK", "Spain", "the Entire EU")
# the color code, appearing in the same order
my_palette <- c("#233F7D", "darkred","#976100", "#297FD5")
ggplot(scale1_data) +
geom_line(data = scale1_data,
aes(x=year, y=revenue, group=market, color=market),
linewidth=1, alpha=0.5) +
geom_line(data = scale2_data,
aes(x=year, y=revenue/5, group=market, color=market),
linewidth=1, alpha=0.5) +
scale_color_manual(name=legend_title,
values = my_palette,
breaks = breaks,
labels = labels) +
scale_x_continuous(breaks = seq(2019, 2021, by = 1)) +
scale_y_continuous("Country revenue (billion EUR)",
sec.axis = sec_axis(~ .*6, name = "EU revenue (billion EUR)")) +
labs(title="Revenues decreased in 2020-2021",
subtitle="in European Markets, compared to 2019") +
theme_minimal()
alpha=0.5
my_color <- "#233F7D"
legend_title <- 'Country/Region'
breaks <- c("deu", "gbr", "esp", "overall")
labels <- c("Germany", "the UK", "Spain", "the Entire EU")
my_palette <- c("#233F7D", "darkred","#976100", "#297FD5")
ggplot() +
geom_hline(yintercept = c(2,4), color="grey", linewidth=0.2) +
geom_line(data = scale1_data,
aes(x=year, y=revenue, group=market, color=market),
linewidth=1, alpha=alpha) +
geom_line(data = scale2_data,
aes(x=year, y=revenue/5, group=market, color=market),
linewidth=1) +
scale_color_manual(name = legend_title,
values = my_palette,
breaks = breaks,
labels = labels) +
scale_x_continuous(breaks = seq(2019, 2021, by = 1)) +
scale_y_continuous("Country revenue (billion EUR)",
sec.axis = sec_axis(~ .*6, name = "EU Revenue (billion EUR)")) +
coord_cartesian(xlim = c(2019, 2021),
ylim = c(0, 6.5),
expand=FALSE) +
labs(title=stringr::str_interp("<span style='color:${my_color}'>**Revenues decreased**</span> in 2020-2021"),
subtitle="in European Markets, compared to 2019",
y="Revenue (billion Euro)") +
theme_minimal() +
theme(plot.title=element_markdown(size=18, color='#535353'),
plot.subtitle=element_text(size=14),
text = element_text(color='#818181'),
panel.grid = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
aspect.ratio = 0.75)