1

I have a question about how to change the legend text, I tried a few answers from this site and non of them worked. Here is the example code:

counts <- c(18,17,15,20,10,20,25,13,12)
time <- c(1, 1.3, 1.11, 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))

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")+
  theme(legend.position = "bottom")

The result is: enter image description here As some answers suggested to use the scale_fill_manual() or scale_fill_discrete(). I tried these solutions but none of them changed the legend text, such as the code below:

counts <- c(18,17,15,20,10,20,25,13,12)
    time <- c(1, 1.3, 1.11, 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))
    
    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")+
      theme(legend.position = "bottom")+
      scale_fill_discrete("", labels=c('women', 'men'))

Any suggestions?

2
  • 1
    You are using the color aesthetic, so you should use scale_color_discrete
    – bouncyball
    Commented Sep 1, 2020 at 18:57
  • 1
    Thanks a lot. I tried and it worked!!
    – Jiang Xu
    Commented Sep 1, 2020 at 18:59

1 Answer 1

4

You are using scale_fill_discrete, while you should use scale_colour_discrete... This should work:

counts <- c(18,17,15,20,10,20,25,13,12)
time <- c(1, 1.3, 1.11, 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))

ggplot(myDF, aes(counts, time, colour = sex)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) +
  ggtitle("Long-Term Gain in Speech Rate") +
  xlab("Baseline Speech Rate") +
  ylab("Mean Speech Rate Gain")+
  theme(legend.title = element_blank(),
        legend.position = "bottom") +
  scale_colour_discrete(labels = c('Women', 'Men'))

Here is the output:

enter image description here

2
  • 1
    Thanks a lot. It worked and I learned something new from this question and answers.
    – Jiang Xu
    Commented Sep 1, 2020 at 19:10
  • Good luck @JiangXu! Commented Sep 1, 2020 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.