In this article, you will learn how to change a ggplot legend order.
Related Book
GGPlot2 Essentials for Great Data Visualization in RPrerequisites
Load the ggplot2 package and set the theme function theme_classic()
as the default theme:
library(ggplot2)
theme_set(
theme_classic()
)
Basic plots
p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(aes(color = Species)) +
scale_color_manual(values = c("#E7B800", "#2E9FDF", "#FC4E07"))
p
Reorder legend labels
Changing the order of legend labels can be achieved by reordering the factor levels of the Species
variable mapped to the color
aesthetic.
- Reorder the factor levels:
# Default order
levels(iris$Species)
## [1] "setosa" "versicolor" "virginica"
# Reverse the order as follow
iris$Species <- factor(iris$Species, levels = rev(levels(iris$Species)))
# Or specify the factor levels in the order you want
iris$Species <- factor(iris$Species, levels = c("virginica", "versicolor", "setosa"))
- Create the plot:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(aes(color = Species)) +
scale_color_manual(values = c("#E7B800", "#2E9FDF", "#FC4E07"))
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
Hi,
how do I change the order not in the legend but in the graph itself? I’ve created a scatter plot but the factor that gives the colour is mixed up. I’d like to have high-medium-low but it is in high-low-medium.
Any ideas?
Thanks in advance