Search code examples
rggplot2axis-labelsggridges

R ggridges plot - Showing y axis ticks and labels


I am trying to generate overlay density plots over time, comparing densities of males vs. females. Here is my output: enter image description here

I am following the Australian athletes height example from https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html. Here is my code:

ggplot(math_dat, aes(x = order_math, y = time, color = gender, point_color = gender, fill = gender)) +
  geom_density_ridges(
    jittered_points = TRUE, scale = .95, rel_min_height = .01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0)
  ) +
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0), name = "Rankings") +
  scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
  scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#D55E00A0", "#0072B2A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)

My problem is I am unable to generate any Y axis tick values and the example doesn't display any either. Any ideas how to get Y axis tick marks to display? Here is some sample data similar to mine:

## generating dataset
order_math<-c(1,2,1,2,3,3,1,2,3,1,2,3)
gender<-c("M","F","M","M","M","F","F","M","F","M","M","F")
time<-c(1,1,2,3,3,2,1,2,3,2,3,1)
sample<-data.frame(order_math,gender,time)

UPdate: After @Tomasu's suggestions I have updated my code, but it does not run:

ggplot(math_dat, aes(x = order_math, y = time, color = gender, point_color = gender, fill = gender)) +
  geom_density_ridges(
    jittered_points = TRUE, scale = .95, rel_min_height = .01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0)
  ) +
  scale_y_reverse(limits = c(1000, 500, 100),expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0), name = "Rankings") +
  scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
  scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#D55E00A0", "#0072B2A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)+
  theme(
    axis.ticks = element_line(size=0.5),         # turn ticks back on
    axis.ticks.length = grid::unit(5, "pt"),     # set length
    axis.ticks.y = element_line(colour = "red"), # define tick line color
    axis.text.y = element_text(vjust = .4)       # center text with tick
  )

Solution

  • An easy solution to this problem would be to use a theme_ that includes the axis ticks as theme_ridges() has them turned off. Just removing that theme all together and using the base ggplot2 theme achieves the desired outcome.

    However, let's say we still want to use theme_ridges() and just turn ticks back on. This can be achieved with a theme() edit after the theme_ridges().

    I'm using the example in the link provided as I couldn't get your sample data to work properly.

    library(ggplot2)
    library(ggplot2movies)
    library(ggridges)
    
    ggplot(movies[movies$year>1912,], aes(x = length, y = year, group = year)) +
      geom_density_ridges(scale = 10, size = 0.25, rel_min_height = 0.03) +
      theme_ridges() +
      theme(
        axis.ticks = element_line(size=0.5),         # turn ticks back on
        axis.ticks.length = grid::unit(5, "pt"),     # set length
        axis.ticks.y = element_line(colour = "red"), # define tick line color
        axis.text.y = element_text(vjust = .4)       # center text with tick
      ) +
      scale_x_continuous(limits = c(1, 200), expand = c(0, 0)) +
      scale_y_reverse(
        breaks = c(2000, 1980, 1960, 1940, 1920, 1900),
        expand = c(0, 0)
      ) +
      coord_cartesian(clip = "off")
    

    Created on 2021-05-11 by the reprex package (v1.0.0)