乱世@小熊 2013-01-31 09:31 采纳率: 25%
浏览 909
已采纳

如何在 ggplot 中更改传奇标题

I have the following plot like below. It was created with this command:

library(ggplot2)

df <- data.frame(cond = factor(rep(c("A", "B"), each = 200)), 
                 rating = c(rnorm(200), rnorm(200, mean=.8)))

ggplot(df, aes(x=rating, fill=cond)) + 
geom_density(alpha = .3) +
xlab("NEW RATING TITLE") +
ylab("NEW DENSITY TITLE")

Now next thing I want to do is to modify the legend title from cond into NEW LEGEND TITLE.

So what I did is to just add the following line add the end of the above code:

+labs(colour="NEW LEGEND TITLE")

But it doesn't work. What's the right way to do it?

enter image description here

转载于:https://stackoverflow.com/questions/14622421/how-to-change-legend-title-in-ggplot

  • 写回答

8条回答 默认 最新

  • 乱世@小熊 2013-01-31 09:35
    关注

    This should work:

    p <- ggplot(df, aes(x=rating, fill=cond)) + 
               geom_density(alpha=.3) + 
               xlab("NEW RATING TITLE") + 
               ylab("NEW DENSITY TITLE")
    p <- p + guides(fill=guide_legend(title="New Legend Title"))
    

    (or alternatively)

    p + scale_fill_discrete(name = "New Legend Title")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统