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.
Leave Customizing Color Palette - ggplot Tutorial 6 to:
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
- Moments That Made Me a Better Splinterlands Player
- Splinterlands Winterfest Gift Box Opening!!🎁
- The Path to Power in Splinterlands 💪
- Finally Got a Grove Doomblade...in GOLD!!
- Splinterlands Arena Architect - Choosing the Winning Strategy!!⚔️ 🛡️
- Splinterlands Card Spotlight: A'lan Musketeer🏹
- Splinterlands Modifier Mastery 🧙♀️🔮⚔️
- Splinterlands Match Analysis ⚔️🔥💥
- Splinterlands Harvest Heroes - The Cards and Community that Make The Game Great!
- Splinterlands Strategy: Double Strike + Poison 🧪☠️
- Just Passed the Cardano Blockchain Certification!!🎉 --- Was It Worth It?
- Splinterlands Inaugural Land Card - My Initial Thoughts
- Learning from Defeat in Splinterlands⚔️🛡️
- Splinterlands Hidden Gems: Ironwing Juggernaut🦉
- Ulundin Overseer and Commander Slade : A Winning Combo⚔️
- Frontier Format is (Still) Amazing!!
- Splinterlands Strategy - Know the Ruleset!!! ⚔️🧙
- Halfling Refugee: A 1 Mana Powerhouse💪
- Splinterlands Strategy - Using Taunt to Protect Valuable Units!
- Lord Thanalor's Last Stand⚔️ - An Incredible 4 vs 1 Victory!!!😲