ggboxplot()

Alternatively, you can use the ggboxplot() function from the library ggpubr: First, install the library and then activate it. Now, we will display working hours by gender.

ggbox <- ggboxplot(
  pss,
  x = "gndr",              
  y = "wkhtot",             
  color = "ivory4",
  fill = "gndr",
  palette = c(
    "lightblue", 
    "pink"
  ),
  ylab = "Total work hours (incl. overtime)",
  xlab = "Gender (1:male, 2:female)",
  na.rm = TRUE
) 

ggbox

Here, we can make fine adjustments easily: For example, we can add a horizontal line (geom_hline()) to mark the mean for each group.

ggbox +
  geom_hline(
    aes(
      yintercept = mean(
        wkhtot[gndr == "male"], 
        na.rm = TRUE
      )
    ),
    lty = 2,
    lwd = 1,
    color = "lightblue") + 
  geom_hline(
    aes(
      yintercept = mean(
        wkhtot[gndr == "female"], 
        na.rm = TRUE
      )
    ),
    lty = 2,
    lwd = 1, 
    color = "pink"
  ) +
  theme(legend.position = "none")

Copy the code into your script and try different numerical values for the lwd and lty arguments.

What do the arguments lwd and lty stand for?

Next, you can explore rainclouds as a better visualization option! However, this is optional!