Amber K avatar

Customizing Color Palette - ggplot Tutorial 6

algoswithamber

Published: 06 Aug 2025 › Updated: 06 Aug 2025Customizing Color Palette - ggplot Tutorial 6

Customizing Color Palette - ggplot Tutorial 6

In a previous post, we learned how to automatically set color palettes for our plots in R using the ggsci package. We got some phenomenal results, but what if we want to manually select specific colors? That's what we're going to learn how to do in today's video.

Getting Started

As with the previous tutorials, we will want to make sure that we have the tidyverse and palmerpenguins packages downloaded and installed on our system and pulled into the libraries using the following commands.

install.packages("tidyverse")
install.packages('palmerpenguins')

library(tidyverse)
library(palmerpenguins)

Once we have those packages pulled into the workspace, we will start by creating a basic scatter plot where. In addition to specifying the x and y axis, we will pass in the color = island command to create a scatterplot where each observation is color-coded to match the island it came from. Everything up to this point is a review, and we are just using the default color scheme that comes with R.

ggplot(data = penguins, aes (x = bill_length_mm, y = bill_depth_mm,
                             color = island))+
  geom_point()

image.png

Manually Setting Colors

To manually select each color pair, we have to use the scale_color_manual command. Inside of this, we will pass in a vector of values. Notice that we are assigning each different element in the data set to a different color. For example, there are three different islands in the Penguins data set. We are selecting the color that we want each island to be.

image.png

ggplot(data = penguins, aes (x = bill_length_mm, y = bill_depth_mm,
                             color = island))+
  geom_point()+scale_color_manual(
    
    values = c(
    "Biscoe" = "purple",
    "Dream" = "orange",
    "Torgersen" = "darkgreen")
    
  )

image.png

NOTE: We do not have to set each different factor level equal to its own color. If we only pass in a single factor level and assign it to a color, then that factor level will change colors, but everything else will remain the default color. This can be a great way to highlight one specific group for emphasis.

ggplot(data = penguins, aes (x = bill_length_mm, y = bill_depth_mm,
                             color = island))+
  geom_point()+scale_color_manual(
    
    values = c(
    "Biscoe" = "orange"
   )
    
  )

This plot shows only penguins from Biscoe in orange, but the other two islands are both grey.

image.png

Summary

As always, thank you so much for taking the time to read my work! I hope you found the tutorial helpful.

Leave Customizing Color Palette - ggplot Tutorial 6 to:

Written by

Ph.D.. student who loves finance, programming, and making the world a better place.

Read more #stem posts


Best Posts From Amber K

We have not curated any of algoswithamber's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Amber K