This article describes how create a ggplot with gradient color. You will learn how to change the ggplot2 default gradient color, as well as, how to set gradient between two or n colors.
Contents:
Loading ggplot2
The following R code loads the ggplot2 R package and set the default plot theme to theme_minimal()
.
library(ggplot2)
theme_set(theme_minimal())
Default ggplot gradient colors
For gradient colors, you should map the map the argument color
and/or fill
to a continuous variable. The default ggplot2 setting for gradient colors is a continuous blue color.
In the following example, we color points according to the variable: Sepal.Length
.
sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(color = Sepal.Length))
sp
Key functions to change gradient colors
The default gradient colors can be modified using the following ggplot2 functions:
scale_color_gradient()
,scale_fill_gradient()
for sequential gradients between two colorsscale_color_gradient2()
,scale_fill_gradient2()
for diverging gradientsscale_color_gradientn()
,scale_fill_gradientn()
for gradient between n colors
Set gradient between two colors
Change the colors for low
and high
ends of the gradient:
# Sequential color scheme.
# Specify the colors for low and high ends of gradient
sp + scale_color_gradient(low = "blue", high = "red")
# Diverging color scheme
# Specify also the colour for mid point
mid <- mean(iris$Sepal.Length)
sp + scale_color_gradient2(midpoint = mid, low = "blue", mid = "white",
high = "red", space = "Lab" )
Note that, the functions scale_color_continuous()
and scale_fill_continuous()
can be also used to set gradient colors.
Set gradient between n colors
In the example below, we’ll use the R base function rainbow()
to generate a vector of 5 colors, which will be used to set the gradient colors.
sp + scale_color_gradientn(colours = rainbow(5))
Conclusion
This tutorial shows how to set gradient colors in ggplot2.
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments