rggplot2naggridges

How to omit to plot NA levels in geom_density_ridges2()


This question is related to this extend axis to 0 with geom_density_ridges2.

Here the OP asks to extend the x axis to show 0, 1, 2 to the existing scale. While @stefan posted the correct answer using limits = c(-.5, 9.5), I tried an alternative way to add the lacking 0, 1, 2, as new rows to value column of df adding this line to the code complete(value = seq(0, max(value), 1)) resulting :

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggridges)

df %>%
  complete(value = seq(0, max(value), 1)) %>%
  ggplot(aes(x = value, y = item, group = item)) +
  geom_density_ridges2(aes(fill = item), 
                       stat = "binline", 
                       binwidth = 1, 
                       scale = 0.95) +
  geom_text(
    stat = "bin",
    aes(
      y = group + 0.95*stat(count/max(count)),
      label = ifelse(stat(count) > 0, stat(count), "")
    ),
    vjust = -0.5, size = 3, color = "black", binwidth = 1
  ) +
  scale_x_continuous(
    breaks = 0:9, 
    name = "Answer (higher = better)"
  ) +
  scale_y_discrete(
    expand = expansion(add = 2)
  ) +
  scale_fill_viridis_d() +
  labs(
    title = "Example",
    y = NULL
  ) +
  theme_ridges(grid = FALSE) +
  theme(
    axis.title.x = element_text(hjust = 0.5),
    axis.title.y = element_text(hjust = 0.5),
    legend.position = "none",
    plot.title.position = "plot",
    plot.title = element_text(face="bold")
  )

enter image description here

How can I remove the NA level while keeping the values for the x axis in the plot above?

Desired output: enter image description here

data:

df <- df <- structure(list(id = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 
7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 
16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 
24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29), item = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"), class = "factor"), 
    value = c(8, 9, 4, 5, 8, 9, 7, 9, 8, 8, 7, 6, 9, 8, 9, 9, 
    9, 9, 8, 9, 9, 9, 8, 9, 8, 8, 7, 8, 8, 8, 7, 9, 8, 9, 8, 
    9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 7, 9, 9, 8, 9, 8, 9, 7, 9, 
    7, 7, 7, 8)), row.names = c(NA, -58L), class = c("tbl_df", 
"tbl", "data.frame"))

Solution

  • You could drop the NA category by setting the limits in scale_Y_discrete to include only the desired "real" categories:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    library(ggridges)
    
    df %>%
      complete(value = 0:9) %>%
      ggplot(aes(x = value, y = item, group = item)) +
      geom_ridgeline(
        aes(fill = item),
        stat = "binline",
        binwidth = 1,
        scale = 0.95
      ) +
      geom_text(
        stat = "bin",
        aes(
          y = group + 0.95 * stat(count / max(count)),
          label = ifelse(stat(count) > 0, stat(count), "")
        ),
        vjust = -0.5, size = 3, color = "black", binwidth = 1
      ) +
      scale_x_continuous(
        breaks = 0:9,
        name = "Answer (higher = better)",
        expand = c(0, 0)
      ) +
      scale_y_discrete(
        limits = c("A", "B"),
        expand = expansion(add = 1)
      ) +
      scale_fill_viridis_d() +
      labs(
        title = "Example",
        y = NULL
      ) +
      theme_ridges(grid = FALSE) +
      theme(
        axis.title.x = element_text(hjust = 0.5),
        axis.title.y = element_text(hjust = 0.5),
        legend.position = "none",
        plot.title.position = "plot",
        plot.title = element_text(face = "bold")
      )
    

    enter image description here