Coloring

So far, our bar chart is quite dull: the axis labels are unclear, and in gray, it looks dreary. Let’s change that now. It is important that if we follow the convention in ggplot, we can omit explanatory parts like data = ... or mapping = .... This means less writing! We simply add color by including the argument color within aes() and specifying which variable should determine the color.

First, let’s add some color:

barplot <- ggplot(
  pss, 
  aes(
    edu, 
    color = edu
  )
) +
  geom_bar()

barplot

We see that the color argument only changes the outline. To change the fill, we need to use the fill argument:

barplot <- ggplot(
  pss, 
  aes(
    edu, 
    fill = edu
  )
) +
  geom_bar() 

barplot

Now we have made the graph colorful. However, you can also set the color values yourself if you want to determine how colorful the graph will be. You can do this by using the fill argument in the geom_bar() function. There, you specify as many colors as you need (no more and no less!). An overview of common color codes can be found here:

  • Rcolor.pdf (209 KB)
  • barplot <- ggplot(
      pss, 
      aes(
        edu, 
        fill = edu
      )
    ) +
      geom_bar(
        fill = c(
          "steelblue", 
          "darkgoldenrod",
          "seagreen", 
          "red4", 
          "orange",
          "darkslategray2"
        )
      ) 
    
    barplot

    But now you want the second bar to be blue. How can you change that?

    You can also store color codes in an object (vector) and then call it in the ggplot() function. This keeps the plot code more organized. It is important that the vector must have the exact same length as the number of groups.

    educol = c(
      "steelblue", 
      "darkgoldenrod",
      "seagreen", 
      "red4", 
      "orange",
      "darkslategray2"
    )
    
    barplot <- ggplot(
      pss, 
      aes(
        x = edu, 
        fill = edu
      )
    ) +
      geom_bar(fill = educol) 
    
    barplot

    When using a color vector, you can also use color vectors that differ from the exact number of categories. If there are too few colors in the color vector, the colors will repeat. However, you cannot use this in the fill argument in the geom_bar() function; instead, you use the additional function scale_fill_manual() for that:

    # a colourblind-friendly palettes
    cbp1 <- c(
      "#999999", 
      "#E69F00",
      "#56B4E9",
      "#009E73",
      "#F0E442", 
      "#0072B2",
      "#D55E00",
      "#CC79A7"
    )
    
    barplotCb <- ggplot(
      pss, 
      aes(
        edu, 
        fill = edu
      )
    ) + 
      geom_bar() +
      scale_fill_manual(values = cbp1)
    
    barplotCb

    When you cannot submit documents digitally, you often use gray graphics to save costs. For this purpose, there is the additional function scale_fill_grey(), which automatically outputs the plot in grayscale:

    barplotGray <- ggplot(
      pss, 
      aes(
        edu, 
        fill = edu
      )
    ) + 
      geom_bar() +
      scale_fill_grey()
    
    barplotGray

    There are also some libraries that contain pre-made color palettes. For example: RColorBrewer or beyonce. Here, we will first use RColorBrewer. RColorBrewer is easy to install, while the library beyonce is installed via a developer tool. Many libraries are still in the development stage and are not officially available yet, but they can still be loaded and used through a developer tool.

    install.packages("RColorBrewer")
    
    # Installation Entwicklertool
    install.packages(
      "devtools",
      dependencies = TRUE
    )
    
    # Install beyonce palette
    devtools::install_github("dill/beyonce")

    With RColorBrewer, you can display the color palettes:

    library("RColorBrewer")
    
    display.brewer.all()

    To use one of these palettes from RColorBrewer, you use the function scale_fill_brewer() and specify the color palette within it:

    barplotBrewer <- ggplot(
      pss, 
      aes(
        edu, 
        fill = edu
      )
    ) + 
      geom_bar() +
      scale_fill_brewer(palette = "Dark2")
    
    barplotBrewer

    To use the beyonce color palette, you proceed differently: First, let’s output all color palettes.

    library("beyonce")
    
    # Ausgabe aller Farbpaletten [beginnend bei 1]
    par(mfrow=c(26,5))
    for(i in 1:130) print(beyonce_palette(i))

    In the example, we choose color palette 25. To call the respective color palette, use the beyonce_palette() function from the library beyonce, and as the only argument, provide the number of the color palette. To use it now, use the additional function scale_fill_manual() in ggplot.

    barplotBeyonce <- ggplot(
      pss, 
      aes(
        edu, 
        fill = edu
      )
    ) +
      geom_bar() +
      scale_fill_manual(values = beyonce_palette(25)) 
    
    barplotBeyonce

    Let’s go and add value labels!