如何删除ggplot2中的图例标题?
我有一个关于ggplot2中的图例的问题。
假设我有一个关于两个农场的两种不同颜色的平均胡萝卜长度的假设数据集:
carrots<-NULL carrots$Farm<-rep(c("X","Y"),2) carrots$Type<-rep(c("Orange","Purple"),each=2) carrots$MeanLength<-c(10,6,4,2) carrots<-data.frame(carrots) 我做一个简单的酒吧情节:
 require(ggplot2) p<-ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) + geom_bar(position="dodge") + opts(legend.position="top") p 
我的问题是:有没有办法从图例中删除标题('types')?
谢谢!
您可以通过将其作为第一个parameter passing给一个比例来修改图例标题。 例如:
 ggplot(carrots, aes(y=MeanLength, x=Farm, fill=Type)) + geom_bar(position="dodge") + theme(legend.position="top", legend.direction="horizontal") + scale_fill_discrete("") 
 还有一个捷径,即labs(fill="") 
 由于您的图例位于图表的顶部,因此您可能还希望修改图例方向。 你可以使用opts(legend.direction="horizontal")来做到这一点。 

 我发现最好的select是使用+ theme(legend.title = element_blank())作为用户“gkcn”logging。 
 对于我(在2015年3月26日)使用先前build议的labs(fill="")和scale_fill_discrete("")删除一个标题,只添加在另一个图例中,这是没有用的。 
 你可以使用labs : 
 p + labs(fill="") 

 我唯一的方法是使用legend.title = theme_blank() ,我认为这是最方便的变种与labs(fill="")和scale_fill_discrete("") ,这在某些情况下也是有用的。 
 ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) + geom_bar(position="dodge") + opts( legend.position="top", legend.direction="horizontal", legend.title = theme_blank() ) 
PS 文档中有更多有用的选项。
 你已经有了两个很好的select,所以这是另一个使用scale_fill_manual() 。 注意这也可以让你很容易地指定条的颜色: 
 ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) + geom_bar(position="dodge") + opts(legend.position="top") + scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple")) 
如果您使用的是ggplot2(版本1.0)的最新版本(截至2015年1月),那么应该如下工作:
 ggplot(carrots, aes(y = MeanLength, x = Farm, fill = Type)) + geom_bar(stat = "identity", position = "dodge") + theme(legend.position="top") + scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple"))