I've been learning R for the past few months and I've struggled with something that I couldn't figure out.
I have a really simple question, how do I display percentiles 20 and 80 instead of 25 and 75 (or Q1/Q3) in a boxplot while using tidyverse
?
I have tried to find documentation about it in the R Graph Gallery and in the tidyverse help and a lot other sites, but I couldn't reproduce. Usually the examples are with only 1 box, but I have a 7 to be shown.
Here is a sample of my data:
dataset <- structure(
list(
PM1 = c(0.4, 6.2, 5.1, 7.8, 8, NA, NA, 5.2),
PM2 = c(2, 8, 5.6, 8, NA, 6.4, 10.3, 7),
PM3 = c(NA, 7.2, 4.8, 4.4, NA, NA, 10.3, 5.9),
PM4 = c(1.2, 8.7, 5.4, NA, NA, NA, NA, NA),
PM5 = c(3.5, NA, 1.9, 2.2, NA, 3.5, 9.4, 0.3),
PM6 = c(1.3, NA, 1.1, NA, NA, 2.8, NA, NA),
PM7 = c(NA, NA, NA, 0.4, NA, NA, 8.8, 0.6)),
row.names = c(NA, -8L),
class = c("tbl_df", "tbl", "data.frame")
)
I can make the boxplot with this different quantiles using qboxplot
, here's the code that I used:
library(qboxplot)
dataset %>%
qboxplot(
main = "Dissolved Oxygen",
probs = c(0.20, 0.50, 0.80),
ylim = c(0, 12),
ylab = "mg/L",
xlab = "Monitoring Points"
)
I have searched for something similar to probs = c(0.20, 0.50, 0.80)
from the qboxplot
package in the ggplot2
but I found different approaches that I couldn't reproduce, like here, here and here.
library(tidyverse)
dataset %>%
pivot_longer(
cols = everything(),
names_to = "monitoring_point",
values_to = "oxigenio_dissolvido"
) %>%
ggplot(
aes(x = monitoring_point,
y = oxigenio_dissolvido)
)+
stat_boxplot(
geom = "errorbar",
width = 0.3,
position = position_dodge(width = 0.65)
)+
geom_boxplot()+
labs(title = "Dissolved Oxygen",
y = "oxigenio_dissolvido (mg/L)")+
scale_y_continuous(
expand = expansion(mult = c(0,0)),
limits = c(0, 12)
)+
theme_bw()+
theme(
plot.title = element_text(hjust = 0.5)
)
I think I'm close to my desired output, but I really didn't get how to change the hinges. Thank you very much in advance for helping me!