【问题标题】:Legend Trouble in R- how to change legend text in ggplot2R中的图例问题-如何更改ggplot2中的图例文本
【发布时间】:2017-02-08 16:05:20
【问题描述】:

我有一个灰色的情节,情节内有图例。但是,图例中的标签是错误的。如果我按照建议更改图例标签,即使不更改主题,我也会以某种方式回到默认颜色。必须有更好的方法。传说中的“sex”为“Method”,“m”为“1”,“f”为“2”,情节仍然是灰色的,将是一个巨大的改进。

require(ggplot2)
counts <- c(18,17,15,20,10,20,25,13,12)
time <- c(1, 1.3, 1.1, 1, 1, 1, 1, 1.3, 1.1)
sex <- c("m","f","m","f","m","f","m","f","m")
print(myDF <- data.frame(sex, counts, time))

gTest <- ggplot(myDF, aes(counts, time, color=sex)) +
    geom_point(size = 3)+geom_smooth(method="lm", se=F) +
    ggtitle("Long-Term Gain in Speech Rate")+
    xlab("Baseline Speech Rate") +
    ylab("Mean Speech Rate Gain")

谢谢!这会改变标题

gTest + scale_colour_grey(start = .3, end = .7) + guides(color=guide_legend(title="Method")) + theme_bw()+ theme(legend.position=c(.9,.9), legend.background=element_rect(fill="white", size=0.5, linetype="solid", colour ="white"))

【问题讨论】:

  • 这是您要找的吗? gTest + scale_colour_grey(start = .3, end = .7) + guides(color=guide_legend(title="Method")) + theme_bw()+ theme(legend.position=c(.9,.9), legend.background=element_rect(fill="white", size=0.5, linetype="solid", colour ="white"))
  • 这改变了标题,情节仍然是灰色的。这是非常好的。迈克,你知道改变因子名称(“m”是“1”,“f”是“2”)吗?
  • 当然,对措辞有点困惑,但这里是更改图例标签+标题的代码:gTest + scale_colour_grey(start = .3, end = .7,labels=c("2","1")) + guides(color=guide_legend(title="Method")) + theme_bw()+ theme(legend.position=c(.9,.9), legend.background=element_rect(fill="white", size=0.5, linetype="solid", colour ="white"))

标签: r ggplot2 legend


【解决方案1】:

为了更改图例标签,您可以编辑对scale_colour_grey() 的调用以包含labels= 参数。要更改图例标题,您可以在 guides() 调用中指定。这应该会产生预期的结果:

gTest + scale_colour_grey(start = .3, end = .7,labels=c("2","1")) +
  guides(color=guide_legend(title="Method")) +
  theme_bw()+
  theme(legend.position=c(.9,.9),
        legend.background=element_rect(fill="white",
                                       size=0.5, linetype="solid", colour ="white"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2020-04-10
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多