水平传说与g_legend [英] Horizontal Legend with g_legend

查看:78
本文介绍了水平传说与g_legend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用通常引用的g_legend函数从ggplot2创建的图形中拉出图例,以便我可以使用grid.arrange将其组织为多个图。我希望传说成为单行的元素,以便我可以轻松地将它放在网格的底部。但是,无论原始图上的格式如何,g_legend总是返回带有单列的图例。有人知道强迫图例只有一行吗?

I'm using the commonly referenced g_legend function to pull a legend out of a graph made with ggplot2 such that I can organize it with multiple plots using grid.arrange. I want the legend to be a single row of elements such that I can easily put it at the bottom of my grid. However, regardless of format on the original plot, g_legend always returns a legend with a single column. Does anyone know of a way to force the legend to only have a single row?

#extract legend
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}


推荐答案

我有同样的问题,我刚刚解决了它。您必须为您的p1.leg添加图例选项:

I had the same problem, and I've just solved it. You have to add legend options to your "p1.leg":

p1.leg <- ggplot(data,aes(v1, v2,colour=v3))+geom_area() + theme(legend.direction = "horizontal", legend.position = "bottom")
p1 <- ggplot(data, aes( v1, v2),

然后运作。

这篇关于水平传说与g_legend的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆