Search code examples
rggplot2factorslevels

Only one level of a four-level factor is showing in ggplot graph


I am using the following code to produce a boxplot with a density plot:

ggplot(data_alk_pan, aes(x = PAN, y = MEAS_RESULT, fill = PAN)) +
  theme(legend.position = "none") +
  theme(text = element_text(size = 10)) +
  scale_x_discrete(labels = paste(levels(data_alk_pan$PAN), "\n(N=",table(data_alk_pan$PAN),")", sep="")) +
  labs(y = "Concentration (mg/l)", x = "Pan") +
  ggdist::stat_halfeye(
    aes(thickness = stat(pdf*n)),
    adjust = 0.5,
    justification = -.5,
    .width = 0,
    point_colour = NA
  ) +
  geom_boxplot(alpha = 0.5, varwidth = TRUE)

It works perfectly for every other factor I have in my data (i.e. if I point to a different column in my dataframe other than PAN) but for some reason it will only show the first level of the factor PAN.

I have checked that R recognises all of the levels in the factor by running the following:

levels(data_alk_pan$PAN)

which returns

[1] "Pan_01"  "Pan_02"  "Pan_03"  "Pan_all"

which is what I would expect. However, only the first level (Pan_01) is showing in the plot. All of the levels have data (i.e. n>0 for all levels).

Any thoughts on what I am missing or what I can do to get all four levels to show in the plot?

Updated query to include some of the data:

> dput(data_alk_pan[1:200, c(8,13)])
structure(list(MEAS_RESULT = c(170, 180, 150, 140, 160, 140, 
330, 130, 130, 210, 130, 130, 310, 400, 200, 160, 160, 160, 170, 
170, 220, 170, 200, 240, 200, 190, 200, 180, 190, 240, 250, 260, 
280, 270, 250, 290, 260, 260, 250, 230, 270, 260, 220, 260, 250, 
310, 270, 86, 270, 150, 290, 290, 300, 290, 270, 210, 280, 300, 
390, 270, 190, 240, 240, 260, 210, 250, 210, 220, 190, 260, 210, 
190, 240, 280, 240, 210, 220, 220, 200, 250, 310, 260, 290, 280, 
180, 280, 310, 310, 300, 300, 280, 190, 180, 180, 200, 210, 180, 
280, 390, 290, 320, 310, 320, 260, 330, 330, 390, 300, 350, 270, 
190, 180, 310, 270, 300, 270, 330, 310, 320, 310, 340, 330, 340, 
270, 340, 57, 360, 28, 320, 170, 21, 370, 320, 330, 430, 340, 
300, 350, 300, 300, 340, 260, 270, 230, 180, 210, 210, 240, 230, 
260, 250, 230, 250, 270, 280, 170, 190, 190, 190, 180, 190, 250, 
250, 260, 270, 180, 260, 260, 260, 260, 250, 260, 280, 290, 130, 
150, 190, 180, 190, 240, 270, 260, 230, 220, 170, 190, 270, 270, 
260, 280, 260, 260, 270, 280, 290, 270, 190, 230, 240, 170), 
    PAN = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 1L, 
    4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Pan_01", 
    "Pan_02", "Pan_03", "Pan_all"), class = "factor")), row.names = c(NA, 
200L), class = "data.frame")

Solution

  • When I run your code there seems no problem. Try to update your packages and run them in a clean environment. Here is a reproducible example:

    library(ggplot2)
    library(ggdist)
    ggplot(data_alk_pan, aes(x = PAN, y = MEAS_RESULT, fill = PAN)) +
      theme(legend.position = "none") +
      theme(text = element_text(size = 10)) +
      scale_x_discrete(labels = paste(levels(data_alk_pan$PAN), "\n(N=",table(data_alk_pan$PAN),")", sep="")) +
      labs(y = "Concentration (mg/l)", x = "Pan") +
      ggdist::stat_halfeye(
        aes(thickness = stat(pdf*n)),
        adjust = 0.5,
        justification = -.5,
        .width = 0,
        point_colour = NA
      ) +
      geom_boxplot(alpha = 0.5, varwidth = TRUE)
    

    Created on 2022-09-01 with reprex v2.0.2