Created
October 27, 2017 20:23
-
-
Save Kumquatum/924a42832b9fc7371703704376c98167 to your computer and use it in GitHub Desktop.
Ordering objects inside ggplot with reorder()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup | |
library(ggplot2) | |
# Sample set | |
fruit <- matrix(c("Apple","186","Banana","93","Elderberry","48","Durian", "28","Cherry", "28"), ncol = 2, byrow = TRUE) | |
colnames(fruit) <- c("Name","Freq") | |
fruit <- as.data.frame(fruit) | |
fruit$Freq <- as.numeric(as.character(fruit$Freq)) | |
# Plot graph | |
ggplot(fruit, aes(reorder(Name, Freq), Freq)) + | |
geom_bar(fill="#00DFC4", stat="identity") + | |
xlab("Fruit") + ylab("Frequency") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment