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")))
    

Answers


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.


Need Your Help

Should an API assign and return a reference number for newly created resources?

api asp.net-web-api conventions restful-architecture

I am building a RESTful API where users may create resources on my server using post requests, and later reference them via get requests, etc. One thing I've had trouble deciding on is what IDs the

Sparql - alphabetical sorting issue

sparql allegrograph

I use AllegroGraph and Sparql to query the results. I am trying to sort based on alphabetical order, but Sparql gives the upper case more preference. Below is an analogous data and query that resem...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.