GGtutorial: Day 3 – Introduction to Colors

[This article was first published on is.R(), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

So, where does ggplot get its colors? If you’ve ever asked ggplot to color on the basis of a factor, you might have beeen surprised by the default color choices.  The fact is, ggplot colors factors on the basis of finding evenly spaced colors around the HSL color circle, shown below: EDIT: ggplot2 actually uses HCL space, which is not the same as the color circle below, but the basic idea of equally-spaced colors is valid.

So, for one color, the default is a salmon/pink color; two colors sees the addition of blue, which is found 180 degrees from the first color. For three colors, the spacing must remain even so each color is now only 120 degrees from one another, and so on. Here’s a plot that shows the color palette ggplot automatically chooses for between 1 and 8 factor levels; enjoy!

This is the output:

To leave a comment for the author, please follow the link and comment on their blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)