如何将绘图图例定位在右下角的绘图外部

时间:2016-11-13 10:05:22

标签: r ggplot2

我正在使用最新的ggplot2,目前正在p + theme(legend.position='right')。功能theme is well documented并且bottomright合并似乎不可能。但是,也许有另一种方法可以实现这一目标?

1 个答案:

答案 0 :(得分:3)

您可以使用2元素数字向量作为位置,例如:

p+theme(legend.position=c(0.85,0)

问题是,它会与绘图区重叠。

如果你想强迫它在一条水平线上,你可以添加:

p+guides(fill = guide_legend(nrow = 1))

output

修改

我使用plot.margin扩展底部的区域,你可以使用参数:

p+
 guides(fill = guide_legend(nrow = 1))+
 theme(plot.margin=unit(c(1,1,4,0.5),"cm"))+
 theme(legend.position=c(0.85,-0.7))

enter image description here

OR

  p+
     theme(plot.margin=unit(c(1,1,4,0.5),"cm"))+
     theme(legend.position=c(0.85,-0.7)

enter image description here

注意

使用Rstudio,当我以一定的宽度*高度导出图像时,我不会得到图例,但是如果我在导出之前拖动并调整视图,如下所示,则可以正常工作。

enter image description here