【问题标题】:Adding a secondary axis to ggplot2 ggridges (joyplot) to show counts in each bin向 ggplot2 ggridges (joyplot) 添加辅助轴以显示每个 bin 中的计数
【发布时间】:2020-11-16 19:38:33
【问题描述】:

我有一个使用ggridges 包制作的岭图,使用stat = 'binline' 参数为每​​个组绘制直方图。我想在左侧的辅助 y 轴上显示每组的计数,每组至少有一个或两个轴刻度。我找不到任何这样做的例子。我知道这并不简单,因为 y 轴是在 ggridges 中构建的。有没有相对简单的方法来做到这一点?

代码:

set.seed(1)
fakedat <- data.frame(x = sample(1:10, size = 100, replace = TRUE),
                      g = letters[1:4])

library(ggplot2)
library(ggridges)

ggplot(fakedat, aes(x = x, y = g)) +
  geom_density_ridges(stat = 'binline', bins = 10, scale = 0.9) +
  theme_ridges()

剧情:

期望的输出是让刻度在左侧从零开始在每组的基线处向上移动,并显示直方图箱中的计数。

【问题讨论】:

    标签: r ggplot2 ridgeline-plot ggridges


    【解决方案1】:

    我认为你这样做很困难。实际上,您要做的是多面直方图。这是一个完全不使用ggridges 的外观非常相似的完整代表:

    set.seed(1)
    fakedat <- data.frame(x = sample(1:10, size = 100, replace = TRUE),
                          g = factor(letters[1:4], letters[4:1]))
    
    library(ggplot2)
    
    ggplot(fakedat, aes(x = x, y = ..count..)) +
      stat_bin(geom = "col",  breaks = 0:11 - 0.5, fill = "gray70") +
      stat_bin(geom = "step", breaks = 0:13 - 1) +
      facet_grid(g ~ ., switch = "y") +
      theme_classic() +
      scale_y_continuous(position = "right") +
      scale_x_continuous(breaks = 0:4 * 3) +
      theme(strip.background    = element_blank(),
            axis.text.x         = element_text(size = 16),
            axis.line.x         = element_blank(),
            axis.ticks.x        = element_line(colour = "gray90"),
            axis.ticks.length.x = unit(30, "points"),
            strip.text.y.left   = element_text(angle = 0, size = 16),
            panel.grid.major.x  = element_line(colour = "gray90"))
    

    reprex package (v0.3.0) 于 2020 年 7 月 27 日创建

    【讨论】:

    • 放弃 ggridges 的一些不错的内置选项将是一种耻辱,但这绝对可以解决问题!
    • @qdread ggridges 是一个非常不错的包,它可以做一些你在 ggplot 中无法做到的事情,但反过来也是如此!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多