Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用cowplot的plot_网格放置图例_R_Ggplot2_Cowplot - Fatal编程技术网

R 使用cowplot的plot_网格放置图例

R 使用cowplot的plot_网格放置图例,r,ggplot2,cowplot,R,Ggplot2,Cowplot,我试图将图例放置在绘图网格下方,但它只是出现在它们的右侧。我认为这个位置是由get\u legend(…)语句中的主题(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 =

我试图将图例放置在
绘图网格下方,但它只是出现在它们的右侧。我认为这个位置是由
get\u legend(…)
语句中的
主题(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)

x=seq(0,10,1)
y=x
z=sqrt(x)
v=对数(x+1)
dat=数据帧(x,y)

p1这应该满足您的要求:

# 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))

bottom\u行啊,不知道图例应该有自己的行。谢谢你的回答!