Boxplot with ggplot2: Trying to lay geom_jitter over code for plot, strange outlier dots

Viewed 267

There a two different codes I researched and am using for making boxplots for my data:

One code (A) is fairly simple, and essentially shows the features I would like in my boxplot: gridlines in the back, distinguishing of my patient groups in control and intervention and by visit, a scaling with numbers on the x and y axis. One problem is that there are weird outliers shown, which I would rather just incorporate into a jitter, where all data points are shown. I would also like to change the measurements of the plot a little, i.e. the x and y axis length/ratios to a more square shape and it would be ideal, if the control and intervention boxes could be further apart from one another. I would also stylistically would like to fill the boxes with a lighter color, as shown in "B".

Thank you in advance!

This is the code for A:

ggplot(df, aes(x=visit,y=weight_v1_3, color=groupci)) +
  geom_boxplot(width=.5) + theme_bw() + scale_color_brewer(palette="Dark2")

The second code I am using (B) has a geom_jitter, which I would really like to incorporate into A.

The code for B:

  df <- df %>% 
  rename_with(~ gsub("\\.", "_", tolower(.x))) 

pal <- c("red", "blue")

g <- ggplot(df, aes(x = groupci, y = weight_v1_3)) +
  geom_boxplot(aes(fill = groupci, fill = after_scale
(colorspace::lighten(fill, .7))), alpha = .5, size = 1.5, outlier.size = 5)
  

g + 
  geom_jitter(aes(color = groupci), width = .1, size = 7, alpha = .5) +
  scale_y_continuous(breaks = 1:9) +
  scale_color_manual(values = pal, guide = "none") +
  scale_fill_manual(values = pal, guide = "none")

Sample data:

structure(list(pseudonym = c(1L, 2L, 4L, 5L, 6L, 7L, 3L, 8L, 
9L, 10L, 11L, 1L, 2L, 4L, 5L, 6L, 7L, 3L, 8L, 9L, 10L, 11L, 1L, 
2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), control.0.1. = c(0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), intervention.0.1. = c(1L, 
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), visit = c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), weight.V1.3 = c(60L, 
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 59L, 59L, 59L, 
59L, 59L, 59L, 59L, 59L, 59L, 59L, 59L, 57L, 57L, 57L, 57L, 57L, 
57L, 57L, 57L, 57L)), class = "data.frame", row.names = c(NA, 
-31L))

"A" plot:

A plot

"B" plot:

B plot

1 Answers

You're seeing the outlier points because by default, they are shown in geom_boxplot(). If you would prefer to hide them, you can set outlier.color or outlier.shape to NA within geom_boxplot().

Here's an illustrative example. First, showing boxplots overlaying geom_jitter() with default values. The color of the box plot is black and I've changed the shape of the jitter points to make the outlier points shown via geom_boxplot() more apparent.

library(ggplot2)

df <- diamonds[sample(1:nrow(diamonds), size=500),]

p <-
ggplot(df, aes(x=cut, y=price)) +
  geom_boxplot() + 
  geom_jitter(aes(color=cut), width=0.2, size=2, shape=2)
p

enter image description here

Now, to remove the outliers from the boxplot geom, you can set any of the outlier.shape or outlier.color values to NA and it will remove them.

p1 <-
ggplot(df, aes(x=cut, y=price)) +
  geom_boxplot(outlier.shape = NA) + 
  geom_jitter(aes(color=cut), width=0.2, size=2, shape=2)
p1

enter image description here

You should be able to apply this to your own example (I could not reproduce because you reference columns in the code that do not exist in the data); however, note that removing outliers does not work with outlier.size=NA. You'll have to use outlier.color=NA or outlier.shape=NA.

Related