0

I have overlaid density plots (of boys vs girls) in a ridgeline density plot. The X axis values are whole numbers ranging from 1 to 9 but in the density plot they are smoothed to go from 0 to 10. Also the X axis displays decimal values such as 2.5 and 7.5.

  1. Is there anyway to ensure only whole number values are displayed and no fractional values?
  2. Can I control the display to only range from 1 to 9 and not have the 0 and 10 portionas : in other words only display the portion of the graph between the values 1 and 9?

Here is some sample data:

rank<-c(1,2,3,1,5,7,4,6,8,9,1,2,4,5,6,3,8,7,9,3)
gender<-c("M","M","M","M","M","M","M","M","M","M",
          "F","F","F","F","F","F","F","F","F","F")
time<-c(1,2,3,4,1,2,3,4,3,3,4,4,4,4,1,1,1,1,2,3)
data<-data.frame(rank,gender,time)

Here is the code I am using:

ggplot(math_dat, aes(x = rank, 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("#33FFDE50", "#00663350"), labels = c("Girls", "Boys")) +
  scale_color_manual(values = c("#33FFDE", "#006633"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#33FFDE", "#006633"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#33FFDEA0", "#006633A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)

Here is my plot: enter image description here Thanks!

user2450223
  • 235
  • 2
  • 10
  • For the first, have you tried setting the `breaks` in `scale_x_continuous()`? `limits` may help with the second, but not certain that's what you are looking for. – aosmith Jun 10 '21 at 18:41
  • `scale_x_continuous(expand = c(0, 0), name = "Rankings", breaks = 1:9, limits = c(1, 9))` – Gregor Thomas Jun 10 '21 at 18:44

1 Answers1

0

Try giving explicitly the breaks:

ggplot(data, aes(x = rank, 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", breaks = seq(0,10,by = 2)) + #give the break values here
  scale_fill_manual(values = c("#33FFDE50", "#00663350"), labels = c("Girls", "Boys")) +
  scale_color_manual(values = c("#33FFDE", "#006633"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#33FFDE", "#006633"), guide = "none") +
  coord_cartesian(clip = "off") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#33FFDEA0", "#006633A0"),
      color = NA, point_color = NA)
  )
  ) +
  ggtitle("Ranks over time") +
  theme_ridges(center = TRUE)
Pedro Alencar
  • 1,049
  • 7
  • 20