Boxplot Without Whiskers Using R GGplot2
Two methods are presented below to create a boxplot without whiskers in the R GGplot2 package. The first method is used when the boxplot is generated directly from individual data points, and the second method is used when the boxplot is generated from summary statistics (e.g., median, Q1, Q3, min, and max).
Method 1
In the first method, the coef= parameter in the geom_boxplot() function can be used to remove the whiskers from the boxplot. In the following example, a boxplot (per species) of the sepal width variable was created from the iris dataset.
#Boxplots with Whiskers
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
geom_boxplot()+
theme_light()
The above code creates boxplots with whiskers and outliers as usual. The whiskers and outliers can be removed as shown below
#Boxplot without whiskers but with outliers and data points
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
geom_boxplot(coef=0, outlier.fill="red", outlier.shape=23)+
theme_light()
Coef=0 was used to change the length of the whiskers to 0. The outliers (and some data points) were kept, and their shape and color changed to diamond and red respectively. If needed, the outliers can be removed using one of the following options.
#Remove outliers using outlier.size=-1
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
geom_boxplot(coef=0, outlier.size=-1)+
theme_light()
#Outliers also be removed using outlier.shape=NA
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
geom_boxplot(coef=0, outlier.shape=NA)+
theme_light()
The above methods hide but do not remove the outliers, hence the y-axis range remained unchanged.
Method 2
The second method is based on summary statistics already calculated from the individual data points. We used the summary statistics (median, Q1, Q3, min, and max) to create the boxplots.
#Boxplot with Whiskers- created directly from summary statistics
ggplot(statsPL, aes(x=Species)) +
geom_boxplot(aes(ymin = min,
lower = Q1,
middle = median,
upper = Q3,
ymax = max),
stat = "identity") +
theme_light()
We first calculated the summary statistics: median, Q1, Q3, min, and max. Then we used the data frame (statsPL) with the summary statistics to generate the boxplot.
Note that the length of the whiskers in this example is not 1.5xIQR. The length of the whiskers here indicates the min and max values.
If not useful, the whiskers can be removed by specifying the Q1 value for both lower= and ymin= parameters and specifying the Q3 value for both the upper= and ymax= parameters. This will result in whiskers of length zero as shown below.
#Boxplot without Whiskers- created from summary statistics
ggplot(... +
geom_boxplot(aes(ymin = Q1,
lower = Q1,
middle = median,
upper = Q3,
ymax = Q3),
stat = "identity") +
...