Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify different colors with ggplot

Tags:

r

ggplot2

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size=4)

Suppose you have the above scatterplot. How can you specify that the points that are >= 25 mpg will be plotted red, the one between 20 and 25 green and the 0-20 blue?

Can this be done with ggplot specifically?

like image 538
Pavlos Panteliadis Avatar asked Feb 08 '17 13:02

Pavlos Panteliadis


People also ask

How do you set a specific color in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

What colors are available in Ggplot?

You can read all of them using colors() . rgb() → The rgb() function allows to build a color using a quantity of red, green and blue. An additionnal parameter is available to set the transparency.

Is Ggplot colorblind friendly?

The default colors in ggplot2 can be difficult to distinguish from one another because they have equal luminance. They are also not friendly for colorblind viewers.

How do I add color to a Boxplot in R?

To change the color of box of boxplot in base R, we can use col argument inside boxplot function.


2 Answers

You do this in two steps:

First, you define the groups that should have different colours; either by adding another column to the data frame or inside aes. I’ll use aes here:

aes(wt, mpg, color = cut(mpg, breaks = c(0, 20, 25, Inf)))

Secondly, by specifying a manual colour or fill scale:

scale_color_manual(values = c('blue', 'green', 'red'),
                   limits = c('(0,20]', '(20,25]', '(25,Inf]'))

This specifies which colours to use (values) and which labels to assign them to (limits); these are the names of the grouping generated by cut.

Taken together:

ggplot(mtcars) +
    aes(wt, mpg, color = cut(mpg, breaks = c(0, 20, 25, Inf))) +
    geom_point(size = 4) +
    scale_color_manual(values = c('blue', 'green', 'red'),
                       limits = c('(0,20]', '(20,25]', '(25,Inf]'))

You can improve the legend title by adding the grouping as a separate column to your data, or by providing a guides function call:

guides(color = guide_legend(title = 'mpg range'))
like image 129
Konrad Rudolph Avatar answered Oct 16 '22 04:10

Konrad Rudolph


Sure it can, although this type of work if probably best suited to working with your data frame before ggplot(). You could use ifelse() like this:

library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size = 4, 
               aes(color = ifelse(mpg > 25, "> 25", 
                                  ifelse(mpg > 20, "20-25", "< 20")))) +
  scale_color_manual(values = c("> 25" = "red", "< 20" = "blue", "20-25" = "green"),
                     name = "MPG"  )

You don't need to call guides() to create a title you can pass it to the name = .. argument in scale_color_manual()

like image 29
Nate Avatar answered Oct 16 '22 04:10

Nate