简体   繁体   中英

Change order of boxplots in tidy dataframe with geom_boxplot()

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?

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))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM