3

Using the following script, how can I add a bottom legend box like the image bellow?

enter image description here

start <- c('2002 Q1', '2008 Q4')
end <- c('2003 Q3', '2011 Q2')

dates <- as.yearqtr(seq(as.Date('2002-01-01'), as.Date('2019-06-01'), by='quarter'))

cod <- tibble(start = as.yearqtr(start), end = as.yearqtr(end)) %>%
  filter(start %in% dates) %>%
  mutate(start = as.Date(start)) %>%
  mutate(end = as.Date(end))

dates <- as.Date(dates)

tbl_fz <- tibble(x = dates, fz = 0.5)

plot <- ggplot(data = tbl_fz) +
  geom_rect(data = cod, aes(xmin = start, xmax = end,
                            ymin = 0, ymax = 1, fill = "b"), alpha = 0.9) +
  geom_line(aes(x = x, y = fz), size = 0.5) + 
  ggtitle('') + 
  theme_classic() +
  theme(title = element_text(size = 8),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(0.5, -0.5)) +
  ylab('') +
  xlab('') +
  scale_x_date(date_breaks = '2 year', date_labels = '%Y',
               expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0,0)) +
  scale_fill_manual(name = 'kkkk',
                    values = c('grey'),
                    labels = c('kkkk'))

plot
stefan
  • 90,330
  • 6
  • 25
  • 51
Berto
  • 57
  • 5
  • whats the "b" in fill="b"? You need to replace "b" with the variable name yuo want to show in the legend – morgan121 Aug 21 '20 at 04:27

1 Answers1

3

This could be achieved like so. To put the legend on the bottom I would recommend to use legend.position = "bottom". To add the line to the legend map something on the color aes and use scale_color_manual as you have done for the fill aes. To get a box around the legend use legend.box.background = element_rect(color = "black"). I also added some margin on the top and to the left as the box was partially overlayed. Finally, to get the order of the legends right and e.g. to get a thicker line you can make use of guide_legend to style the legend.

# Packages ----------------------------------------------------------------
library(dplyr)
library(ggplot2)
library(zoo)


start <- c('2002 Q1', '2008 Q4')
end <- c('2003 Q3', '2011 Q2')

dates <- as.yearqtr(seq(as.Date('2002-01-01'), as.Date('2019-06-01'), by='quarter'))

cod <- tibble(start = as.yearqtr(start), end = as.yearqtr(end)) %>%
  filter(start %in% dates) %>%
  mutate(start = as.Date(start)) %>%
  mutate(end = as.Date(end))

dates <- as.Date(dates)

tbl_fz <- tibble(x = dates, fz = 0.5)

plot <- ggplot(data = tbl_fz) +
  geom_rect(data = cod, aes(xmin = start, xmax = end,
                            ymin = 0, ymax = 1, fill = "b"), alpha = 0.9) +
  geom_line(aes(x = x, y = fz, color = "c"), size = 0.5) + 
  ggtitle('') + 
  theme_classic() +
  theme(title = element_text(size = 8),
        plot.title = element_text(hjust = 0.5),
        legend.position = "bottom",
        legend.box.background = element_rect(color = "black"),
        legend.box.margin = margin(t = 1, l = 1)) +
  scale_x_date(date_breaks = '2 year', date_labels = '%Y',
               expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0,0)) +
  scale_fill_manual(values = c('grey'), labels = c('kkkk'))+
  scale_color_manual(values = c('black'), labels = c('llll')) +
  labs(x = NULL, y = NULL, fill = NULL, color = NULL) +
  guides(fill = guide_legend(order = 1), color = guide_legend(order = 2, override.aes = list(size = 2)))

plot

stefan
  • 90,330
  • 6
  • 25
  • 51