- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the order of bars in bar chart in R?
This can be done by setting the levels of the variable in the order we want.
Example
> data <- data.frame(Class=c("Highschool","Highschool","Graduate","Graduate", "Graduate","Graduate","Masters","Masters","Masters","PhD"))
Setting the levels in decreasing order
> data <- within(data, Class <- factor(Class, levels=names(sort(table(Class), decreasing=TRUE)))) > library(ggplot2) > ggplot(data, aes(x = Class)) + geom_bar()
Setting the levels in increasing order
> data <- within(data, Class <- factor(Class, levels=names(sort(table(Class), decreasing=TRUE)))) > ggplot(data, aes(x = Class)) + geom_bar()
Advertisements