gpt4 book ai didi

r - 如何删除ggplot2中的图例标题?

转载 作者:行者123 更新时间:2023-12-03 05:48:48 24 4
gpt4 key购买 nike

我有一个关于 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

我的问题是:有没有办法从图例中删除标题(“类型”)?

谢谢!

最佳答案

我发现最好的选择是使用 + theme(legend.title = element_blank()) 正如用户“gkcn”所说。

对我来说(2015 年 3 月 26 日),使用之前建议的 labs(fill="")scale_fill_discrete("") 删除一个标题,仅是为了添加另一个图例,这没有用。

关于r - 如何删除ggplot2中的图例标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6022898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com