Make a Boxplot Without Whiskers

Make a boxplot without whiskers

Under boxplot pars...

 d <- rnorm(1:100, 100, 10)
boxplot(d, whisklty = 0, staplelty = 0)

whisklty gets rid of the lines or whiskers

staplelty gets rid of the ends or staples

boxplot without whiskers and out points

you can add the staplewex and whisklty parameter to the baseplot like this:

boxplot(rnorm(0:200), main="abundances", xlab="Samples", ylab="Phylum abundance (%)", header=TRUE, cex=1, ylim=c(-5,4.), notch=F, 
staplewex = 0, # remove horizontal whisker lines
outline = F, # remove outlying points
whisklty = 0, # remove vertical whisker lines
staplecol = "white", # just to be totally sure :)
whiskcol = "white" # dito
)

Sample Image

Remove whiskers in box-whisker-plot

We only need to add the argument coef = 0:

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(outlier.shape = NA, coef = 0) # Or outlier.size = -1 instead of outlier.shape = NA

Sample Image

Remove whiskers in boxplot made by ggboxplot (of ggpubr package)?

I cannot find a way implemented in ggboxplot directly to do this, which is a bit strange because it passes the ellipsis to a geom_boxplot call, so I am not sure why the coef=0 does not reach there and supresses the whiskers.
As a stopgap, you can modify the ggplot object created by ggboxplot and remove whiskers that way.
The following function shows this:

ggboxplot_whisker_opt <- function(...)
{
opts <- list(...)
# check if user specified a whiskers argument and set options accordingly
if("whisker" %in% names(opts))
{
whisk <- opts$whisker
opts$whisker <- NULL
} else {
whisk <- TRUE
}
pl <- do.call(ggboxplot,opts) # create plot by calling ggboxplot with all user options
if(!whisk)
{
pl_list <- ggplot_build(pl) # get listed version of ggplot object to modify
pl_list$data[[1]]$ymin <- NA # remove the ymin/max that specify the whiskers
pl_list$data[[1]]$ymax <- NA
pl <- ggplot_gtable(pl_list) # convert back to ggplot object
}
# plot the ggplot and return
plot(pl)
}

We can now call that function with whisker=TRUE/FALSE or without it and it produced plots accordingly:

set.seed(123)
x <- rnorm(100)
labels <- round(runif(100,1,2))
df <- data.frame(labels=labels,
value=x)

ggboxplot_whisker_opt(df,"labels","value")
# is the same as
ggboxplot_whisker_opt(df,"labels","value",whisker=TRUE)

Sample Image

ggboxplot_whisker_opt(df,"labels","value",whisker=FALSE)

Sample Image

Box plot with only one Whisker line?

As has been noted in comments, there's nothing wrong with the boxplots. The fact that there is just one whisker each is a reflection of your data.

Boxplots are an extremely instructive type of graphic: they not only show the location and spread of data but also indicate skewness. The interpretation of the boxplot depends on its ‘syntax’, i.e., its main graphical elements. There are five such elements:

1. bold horizontal line: depicts the median (the middle value of a sorted distribution)
2. box: represents the interquartile range (IQR)
3. whiskers: separate values lying outside the IQR (but still somewhat typical of the data) from outliers
3. empty circles: depict outliers (values surprisingly large or small given all values considered)
4. notches (optional): give a rough impression of the significance of the difference between the medians

The fact that there's just one whisker in each boxplot is, then, due to the extreme skewness of your data: in the case of box 1 the upper limit of the values is the upper limit of the IQR, and in the case of box 2 there exists no value smaller than the median!

Hope this helps.



Related Topics



Leave a reply



Submit