Skip to content

Instantly share code, notes, and snippets.

@Kumquatum
Created October 27, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kumquatum/924a42832b9fc7371703704376c98167 to your computer and use it in GitHub Desktop.
Save Kumquatum/924a42832b9fc7371703704376c98167 to your computer and use it in GitHub Desktop.
Ordering objects inside ggplot with reorder()
# 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