使用 plot_grid of cowplot 放置图例
Placement of legend using plot_grid of cowplot
我试图将图例放在 plot_grid
下方,但它恰好出现在它们的右侧。我认为该职位是由 get_legend(...)
声明中的 theme(legend.position = 'bottom')
控制的。我做错了什么?
要重现的示例代码:
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)
这应该可以满足您的要求:
# your code above
# you'd put the plots in two rows, to have the third in the bottom
library(cowplot)
library(ggplot2)
plot_grid(p1 + theme(legend.position = 'none'),
p2,
legend, labels = c('1', '2'), nrow = 2 )
你也可以试试这个:
bottom_row <- plot_grid(legend)
upper_row <- plot_grid(p1+ theme(legend.position = 'none'), p2, ncol =2, labels = c('1', '2'))
plot_grid(upper_row,
bottom_row,
labels = c('',''), label_size = 12, ncol = 1, rel_heights = c(5,1))
我试图将图例放在 plot_grid
下方,但它恰好出现在它们的右侧。我认为该职位是由 get_legend(...)
声明中的 theme(legend.position = 'bottom')
控制的。我做错了什么?
要重现的示例代码:
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)
这应该可以满足您的要求:
# your code above
# you'd put the plots in two rows, to have the third in the bottom
library(cowplot)
library(ggplot2)
plot_grid(p1 + theme(legend.position = 'none'),
p2,
legend, labels = c('1', '2'), nrow = 2 )
你也可以试试这个:
bottom_row <- plot_grid(legend)
upper_row <- plot_grid(p1+ theme(legend.position = 'none'), p2, ncol =2, labels = c('1', '2'))
plot_grid(upper_row,
bottom_row,
labels = c('',''), label_size = 12, ncol = 1, rel_heights = c(5,1))