Ridgeline Plot

An alternative to histograms are Ridgeline Plots, which also display the density distribution. To create these, we use a function from the library ggridges. Ridgeline Plots always represent split variables, so you need to specify a y-variable in the ggplot() function. Let’s create a Ridgeline Plot of age separated by gender categories.

install.packages("ggridges")
library("ggridges")

To create a Ridgeline Plot, we use the function geom_density_ridges(): Within the ggplot() function, we set gender as the y-variable and specify that the color representation should differ based on the variable gndr (argument fill = gndr):

ridgeline <- ggplot(
  pss, 
  aes(
    agea,
    gndr,
    fill = gndr
  )
) +
  geom_density_ridges(
    scale = 0.9,
    alpha = 0.4
  ) +
  theme_ridges() +
  theme(legend.position = "none")

ridgeline
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_density_ridges()`).

You can easily customize this as well.

ridgeline + 
  labs(
    title = "Ridgeline Plot", 
    x = "Age in years",
    y = ""
  ) + 
  theme_ridges(
    grid = TRUE,
    center_axis_labels = TRUE
  ) + 
  scale_fill_cyclical(
    labels = c(
      "female", 
      "male"
    ),
    values = cbp1,
    guide = "legend",
    name = "Gender"
  ) +
  theme(axis.text.y = element_blank())
## Warning: Removed 157 rows containing non-finite outside the scale range
## (`stat_density_ridges()`).