控制ggplot脊线图中的X轴值

Controlling X axis values in ggplot ridgeline graph

我在脊线密度图中叠加了(男孩与女孩的)密度图。 X 轴值是从 1 到 9 的整数,但在密度图中它们被平滑到从 0 到 10。X 轴也显示小数值,例如 2.5 和 7.5。

  1. 有没有办法确保只显示整数值而不显示小数值?
  2. 我能否控制显示范围仅从 1 到 9 而没有 0 和 10 部分:换句话说,只显示值 1 和 9 之间的图表部分?

这是一些示例数据:

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)

这是我使用的代码:

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)

这是我的情节: 谢谢!

尝试明确给出休息时间:

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)