Skip to content

Instantly share code, notes, and snippets.

@Kumquatum
Created October 27, 2017 20:23
Embed
What would you like to do?
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