【问题标题】:R ggridges plot - Showing y axis ticks and labelsR ggridges plot - 显示 y 轴刻度和标签
【发布时间】:2021-08-01 20:13:01
【问题描述】:

我正在尝试随时间生成叠加密度图,比较男性与女性的密度。这是我的输出:

我正在关注来自https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html 的澳大利亚运动员身高示例。 这是我的代码:

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)

我的问题是我无法生成任何 Y 轴刻度值,并且该示例也不显示任何值。任何想法如何让 Y 轴刻度线显示? 以下是一些与我类似的示例数据:

## 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)

更新: 在@Tomasu 的建议之后,我更新了我的代码,但它没有运行:

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
  )

【问题讨论】:

    标签: r ggplot2 axis-labels ggridges


    【解决方案1】:

    解决此问题的一个简单方法是使用包含轴刻度的theme_,因为theme_ridges() 已将它们关闭。只需将该主题全部移除并使用基本 ggplot2 主题即可达到预期结果。

    但是,假设我们仍然想使用 theme_ridges() 并重新打开滴答声。这可以通过theme()theme_ridges() 之后编辑 来实现。

    我正在使用提供的链接中的示例,因为我无法让您的示例数据正常工作。

    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")
    

    reprex package (v1.0.0) 于 2021-05-11 创建

    【讨论】:

    • 它对我不起作用 :( 我尝试使用 theme() 和 scale_y_reverse 打开刻度。由于某种原因,scale_y_reverse 在使用 scale_y_discrete 运行时会导致错误,但不显示Y 轴标签。
    • 这是我使用 scale_y_reverse 得到的错误:-x 中的错误:一元运算符的参数无效。我已经编辑了我的问题,以展示我如何使用您的建议。
    • 您是否有机会使用您的math_order 数据集而不是您的实际数据?我无法运行您的代码,因为在我尝试使用它时提供的数据错误。
    【解决方案2】:

    我认为您的问题是您需要指定组。

    相关话题:geom_density_ridges requires the following missing aesthetics: y

    user tomasu's answer +1 扩展代码

    library(ggridges)
    library(ggplot2)
    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)
    
    ggplot(sample, aes(x = order_math, y = time, group = time,
                       color = gender, point_color = gender, fill = gender)) +
      geom_density_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
      )
    #> Picking joint bandwidth of 0.555
    

    reprex package (v2.0.0) 于 2021-05-12 创建

    【讨论】:

    • 我希望在 Y 轴上显示相对频率或计数,以及指定 Y 变量级别的标签。这些图都没有这样做,包括示例代码中的原始图。例如,如果 order math=1 的频率约为 1000,而 order math=2 的频率约为 500,我想显示它。即使在 scale_y_discrete 语句中添加了 breaks=c(100,500,1000) 后,我也无法做到这一点
    • @user2450223 这不是 Ridgeline 图的作用。考虑不同的可视化,特别是方面。
    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 2020-11-22
    • 2015-12-18
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多