传说中R-如何在ggplot2中改变图例文本。

时间:2023-02-09 21:39:14

I have a plot in grey with the legend inside the plot. However, the labels in the legend are wrong. If I follow advice to change the legend labels, even without changing the theme, then I'm somehow back to the default colors. There has got to be a better way. "sex" in the legend as "Method", "m" is "1" and "f" is "2" with the plot still gray, would be a huge improvement.

我有一个灰色的情节,里面有传说。然而,图例中的标签是错误的。如果我按照建议更改legend标签,即使不更改主题,我也会以某种方式回到默认的颜色。必须有更好的办法。“性”在传说中是“方法”,“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")

Thanks! This changes the title

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_color_grey (start = .3, end = .7) + guide (color=guide_legend(title="Method")) + theme_bw()+ theme(legend.position=c(.9,.9), legend。背景=element_rect(fill="white", size=0.5, linetype="solid", colour ="white")

1 个解决方案

#1


2  

In order to change the legend labels, you can edit your call to scale_colour_grey() to include a labels= argument. To change the legend title, you can specify this in your guides() call. This should produce the desired result:

为了更改legend标签,可以编辑对scale_color_grey()的调用,以包含一个label =参数。要更改legend标题,您可以在向导()调用中指定它。这将产生预期的结果:

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中改变图例文本。

#1


2  

In order to change the legend labels, you can edit your call to scale_colour_grey() to include a labels= argument. To change the legend title, you can specify this in your guides() call. This should produce the desired result:

为了更改legend标签,可以编辑对scale_color_grey()的调用,以包含一个label =参数。要更改legend标题,您可以在向导()调用中指定它。这将产生预期的结果:

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中改变图例文本。