Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

r - ggplot2: How to adjust fill colour in a boxplot (and change legend text)?

How can I:

  1. Set the fill colors in the boxplot below? I tried the argument "colour" but that failed.
  2. Change the legend text from "0", "1" to something else?

    require(ggplot2)
    ggplot(mtcars, aes(factor(cyl), mpg)) +
        geom_boxplot(aes(fill=factor(vs), colour=c("grey50", "white")))
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of the colour aesthetic, you want to adjust the fill aesthetic. You can handle both of your questions (and much more) by adjusting the scale:

ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(vs))) + 
  geom_boxplot() +
  scale_fill_manual(name = "This is my title", values = c("pink", "green")
                    , labels = c("0" = "Foo", "1" = "Bar"))

The ggplot2 website help page for scale_manual is full of good examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...