Business professionals discussing data charts and graphs in a modern office setting.

A Whole Information to the Default Colours in ggplot2

Posted on
banner 336x280

The ggplot2 package deal has an inventory of default colours that it makes use of for the weather in a plot relying at the choice of general components.

For instance, refer to code displays how one can build a bar plot with 3 bars:

banner 468x60
library(ggplot2)

#build information body
df <- information.body(crew=c('A', 'B', 'C'),
                 issues=c(22, 28, 15))

#build bar plot the use of df
ggplot(df, aes(x=crew, y=issues, fill=crew)) +
  geom_bar(stat = "id")

By way of default, ggplot2 chooses to utility a particular silhoutte of crimson, inexperienced, and blue for the bars.

We will utility the hue_pal() from the scales package deal to take out the latest hex colour codes worn within the plot:

library(scales)

#take out hex colour codes for a plot with 3 components in ggplot2 
hex <- hue_pal()(3)

#show hex colour codes
hex

[1] "#F8766D" "#00BA38" "#619CFF"

Right here’s how one can interpret the output:

  • The hex colour code for the crimson within the plot is #F8766D.
  • The hex colour code for the golf green within the plot is #00BA38.
  • The hex colour code for the blue within the plot is #619CFF.

We will utility additionally utility show_col() from the scales package deal to overlay the hex colour codes on their latest colours:

library(scales)

#take out hex colour codes for a plot with 3 components in ggplot2 
hex <- hue_pal()(3)

#overlay hex colour codes on latest colours
show_col(hex)

ggplot2 hex color codes

And we will utility refer to code to build a plot that displays the default ggplot2 colours for plots with one thru 8 components:

library(scales)

#eager margins of plot branch
par(mai = c(0.1, 0, 0.1, 0), bg = "grey85")

#build plot with ggplot2 default colours from 1 to eight
gc.grid <- line(matrix(1:8, nrow = 8))
for(i in 1:8){
   gc.ramp <- hue_pal()(i)
   plot(c(0, 8), c(0,1),
        kind = "n", 
        bty="n", 
        xaxt="n", 
        yaxt="n", xlab="", ylab="")
   for(j in 1:i){
      rect(j - 1, 0, j - 0.25, 1, col = gc.ramp[j])
   }
}

ggplot2 default colors

And we will utility refer to code to show the hex colour codes for every colour proven within the plot:

library(scales)

#show ggplot2 default hex colour codes from 1 to eight
for(i in 1:8){
  print(hue_pal()(i))
}

[1] "#F8766D"
[1] "#F8766D" "#00BFC4"
[1] "#F8766D" "#00BA38" "#619CFF"
[1] "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"
[1] "#F8766D" "#A3A500" "#00BF7D" "#00B0F6" "#E76BF3"
[1] "#F8766D" "#B79F00" "#00BA38" "#00BFC4" "#619CFF" "#F564E3"
[1] "#F8766D" "#C49A00" "#53B400" "#00C094" "#00B6EB" "#A58AFF" "#FB61D7"
[1] "#F8766D" "#CD9600" "#7CAE00" "#00BE67" "#00BFC4" "#00A9FF" "#C77CFF" "#FF61CC"

Supplementary Assets

Please see tutorials give an explanation for how one can carry out alternative regular operations in R:

A Whole Information to the Easiest ggplot2 Topics
Exchange Legend Dimension in ggplot2
Take away a Legend in ggplot2

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *