I am trying to place the legend below a plot_grid
but it just appears to the right of them. I thought the position was governed by the theme(legend.position = 'bottom')
in the get_legend(...)
statement. What am I doing wrong?
Example code to reproduce:
x = seq(0,10,1)
y = x
z = sqrt(x)
v = log(x+1)
dat = data.frame(x,y)
p1 <- ggplot(dat) + theme_minimal(base_size = 16) + labs(x = 'x', y = 'f(x)') +
geom_line(aes(y=y, x=x, linetype = 'A'), color = 'black', size = 1) +
geom_line(aes(y=z, x=x, linetype = 'B'), color = 'black', size = 1) +
geom_line(aes(y=v, x=x, linetype = 'C'), color = 'black', size = 1) +
scale_linetype_manual(values = c(
'A' = 'solid',
'B' = 'dashed',
'C' = 'twodash')) +
labs(linetype = 'f') +
theme(legend.key.width = unit(1.5, 'cm'), legend.position = 'bottom', aspect.ratio = 1)
p2 <- ggplot(dat) + theme_minimal(base_size = 16) + labs(x = 'x', y = 'f(x)') +
geom_line(aes(y=y, x=x, linetype = 'A'), color = 'black', size = 1) +
geom_line(aes(y=z, x=x, linetype = 'B'), color = 'black', size = 1) +
geom_line(aes(y=v, x=x, linetype = 'C'), color = 'black', size = 1) +
scale_linetype_manual(values = c(
'A' = 'solid',
'B' = 'dashed',
'C' = 'twodash')) +
theme(legend.position = 'none', aspect.ratio = 1)
legend <- get_legend(p1 + theme(legend.position = 'bottom'))
plot_grid(p1 + theme(legend.position = 'none'), p2, legend, labels = c('1', '2'), nrow = 1)