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()

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.

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")
)

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.

Summary
As always, thank you so much for taking the time to read my work! I hope you found the tutorial helpful.
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Consider setting @stemsocial as a beneficiary of this post's rewards if you would like to support the community and contribute to its mission of promoting science and education on Hive.
Congratulations @algoswithamber! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 1750 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOPCheck out our last posts: