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:
"B" plot:



