Is there a way to order the bars in geom_bar()
when y
is just the count of x
?
Example:
ggplot(dat) +
geom_bar(aes(x = feature_1))
I tried using reorder()
but it requires a defined y
variable within aes()
.
Made up data:
dfexmpl <- data.frame(stringsAsFactors = FALSE,
group = c("a","a","a","a","a","a",
"a","a","a","b","b","b","b","b","b","b","b","b",
"b","b","b","b","b","b"))
plot code - reorder is doing the work of arranging by count:
dfexmpl %>%
group_by(group) %>%
mutate(count = n()) %>%
ggplot(aes(x = reorder(group, -count), y = count)) +
geom_bar(stat = "identity")