Reputation: 1489
I have a data that looks like this:
cats = c("cat1", "cat2", "cat3", "cat4")
df = data.frame(a = rnorm(100), b = as.factor(rep(cats, 25)))
When I plot it I get something like this:
ggplot(data = df) + geom_boxplot(aes(x = b, y = a, fill = b))
But what can I do if I'd like them to be in the order cat4, cat3, cat2, cat1 on the x-axis. Or even in a completely different order?
Upvotes: 0
Views: 513
Reputation: 41
It is not mandatory to define the variable as.factor() for ggplot. By default it will recode the variable as.factor, but in this case it will follow the alphabetic order.
However if you want a particular order, you need to define as.factor() and enter the order of the levels.
For example, if you want the boxplots ordered according to their median values:
cats = c("cat1", "cat2", "cat3", "cat4")
df = tibble(a = rnorm(100), b = rep(cats, 25))
library(dplyr)
position <- df %>% group_by(b) %>% summarise(median=median(a)) %>%
arrange(desc(median)) %>% pull(b)
df$b <- factor(df$b,levels=position)
# order_wanted <- c(2,1,4,3)
# levels(df$b) <- paste0("cat",order_wanted)
library(ggplot2)
ggplot(data = df) + geom_boxplot(aes(x = b, y = a, fill = b))
Upvotes: 1