How to Change Order of Boxplots When Using Ggplot2

How to change order of boxplots when using ggplot2?

Have you tried this:

df2$variable <- factor(df2$variable,
levels = c('vph.shr','vnu.shr'),ordered = TRUE)

I just picked an ordering there, since my system is configured slightly differently than yours I suspect, so my 'default ordering' may differ. But you can just switch the position of levels when specifying them.

A few other options, depend on your tastes:

For just reversing the current ordering:

factor(df2$variable,levels = rev(levels(df2$variable)),ordered = TRUE)

or you can use subsetting to specify a specific ordering if you don't want to type out each level by hand:

factor(df2$variable,levels = levels(df2$variable)[1:2],ordered = TRUE)

Ggplot: how to show boxplots in a given order?

If I understood correctly, this shoud work.

library(tidyverse)


# Sample data

df1 <-
tibble(
id = c("A","A","A","A","B","B","B","C","C"),
value = c(1,2,3,5,10,8,1,3,7),
type = "df1"
)


df2 <-
tibble(
id = c("A","A","B","B"),
value = c(4,5,6,8),
type = "df2"
)


df <-
# Create single data.frame
df1 %>%
bind_rows(df2) %>%
# Reorder id by median(value)
mutate(id = fct_reorder(id,value,median))

df %>%
ggplot(aes(id, y = value, fill = type)) +
geom_boxplot()

Sample Image

ggplot2 reorder my boxplot by 80th percentile


  1. The problem of not having the graph in order may be due to NAs, try filtering them previously:

    data <- data %>% filter(!is.na(y))

  2. try FUN = quantile, prob = 0.80, in the reorder function you will end up with:

     ggplot(data, aes(x=reorder(y, x, FUN = quantile, prob=0.80), y)) +
    geom_boxplot(fill="deepskyblue") +
    stat_boxplot(geom ='errorbar', width=0.3) +
    theme_bw()+
    scale_y_continuous(trans="log10", n.breaks = 6)

Order of boxplot in ggplot2

Just do

hdata$Mono<- factor(hdata$Mon, levels = c(12, 1:8))

factor by default orders the levels in ascending order. You can order them yourself by using the levels argument.

How to change the order of boxplot in R?

You can specify the order of factors with the 'levels' argument in the factor() function.

#days column is by default ordered alpabetically
data <- data.frame(days = c(rep("Monday", 20),
rep("Tuesday", 20),
rep("Wednesday", 20),
rep("Thursday", 20)),
value = c( sample(2:5, 20 , replace=T) , sample(6:10, 20 , replace=T),
sample(1:7, 20 , replace=T), sample(3:10, 20 , replace=T) ))

boxplot(data$value ~ data$days)

#change order with 'factor()' and specify order with 'levels' argument
data$days <- factor(data$days, levels = c("Monday", "Tuesday", "Wednesday", "Thursday"))
boxplot(data$value ~ data$days)

Reorder boxplots based on their box size with ggplot2 in R

Do you mean the Interquartile range (IQR())? If so you can do

diamonds %>% 
as.tibble() %>%
ggplot(aes(reorder(cut, price, IQR), price)) +
geom_boxplot()

R Change bar order grouped box-plot (fill-variable)

ggplot uses the order of the factor for this purpose. You can set month as ordered factor either inside ggplot call or change it before, in the data. In that case just add the following line before ggplot call:

df[['month']] = ordered(df[['month']], levels = c('oct 2018', 'nov 2018', 'dec 2018'))


Related Topics



Leave a reply



Submit