【问题标题】:Controlling X axis values in ggplot ridgeline graph控制 ggplot 脊线图中的 X 轴值
【发布时间】:2021-08-27 18:46:06
【问题描述】:

我在山脊线密度图中叠加了(男孩与女孩的)密度图。 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)

这是我的情节: 谢谢!

【问题讨论】:

  • 首先,您是否尝试在scale_x_continuous() 中设置breakslimits 可能对第二个有帮助,但不确定这就是您要找的。​​span>
  • scale_x_continuous(expand = c(0, 0), name = "Rankings", breaks = 1:9, limits = c(1, 9))

标签: r ggplot2 axis-labels x-axis


【解决方案1】:

尝试明确给出中断:

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)

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-08-11
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多