About this question
What function can I use to emulate ggplot2's default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX colors with these colors:
What function can I use to emulate ggplot2's default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX colors with these colors:
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Data Science Expert
Answered on Apr 20, 2021
The default color scheme in ggplot2 i.e., scale_color_hue picks evenly spaced hues around the hcl (Hue-Chroma-Luminance) color wheel starting from 15.
You can emulate ggplot default colors using the following function:
gg_color <- function(n) { hues = seq(15, 375, length = n + 1) hcl(h = hues, l = 65, c = 100)[1:n]}For example:
To plot a bar plot by emulating 5 ggplot colors:
n = 5cols = gg_color(n)y <- 1:5barplot(y, col = cols)Output:
ORn = 5cols = gg_color(n)dev.new(width = 5, height = 5)plot(1:n, pch = 16, cex = 2, col = cols)Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Data Science guides from industry experts
Common Data Science interview questions, answered
Guides, tips and career advice on Data Science from JanBask experts.
Data Science The Best Data Science Projects (Beginner To Advanced)
Discover 15 data science project ideas from beginner to advanced levels. Includes tech stacks, time estimates, & real-use cases.…
Data Science Data Science vs Software Engineering - What you should know?
Compare Data Science vs. Software Engineering - explore skills, tools, roles, salaries, and career scope. Discover which tech…
Data Science Top 15 Deep Learning Algorithms You Must Know in 2025
Discover the top 15 deep learning algorithms powering AI in 2025. Learn how deep learning models, DL models, and deep machine…
Data Science Deep Learning Tutorial Guide for Beginners
Deep Learning, Machine Learning & Artificial Intelligence have become trending words of the IT sector today.