向ggplot2 ggridges(joyplot)添加辅助轴以显示每个bin中的计数 [英] Adding a secondary axis to ggplot2 ggridges (joyplot) to show counts in each bin
问题描述
我有一个用 ggridges
包制作的岭线图,使用 stat ='binline'
参数为每个组绘制直方图.我想在左侧的次要y轴上显示每个组的计数,每组至少有一个或两个轴刻度.我找不到任何完成此操作的示例.我知道这并不简单,因为在 ggridges
中y轴的构造方式.有相对简单的方法吗?
I have a ridge plot made with the ggridges
package, using the stat = 'binline'
argument to plot a histogram for each group. I would like to show the counts for each group on a secondary y-axis at left, with at least one or two axis ticks per group. I cannot find any examples where this has been done. I know it is not straightforward because of the way the y-axis is constructed in ggridges
. Is there a relatively easy way to do this?
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()
情节:
所需的输出将是在每个组的基线处使刻度线从左侧开始,从零开始,并在直方图箱中显示计数.
Desired output would be to have ticks going up the left side that begin at zero at the baseline of each group and show the counts in the histogram bins.
推荐答案
我认为您正在这样做.实际上,您要尝试的是刻面直方图.这是一个完全相似的外观,完全不使用 ggridges
:
I think you're doing this the hard way. Effectively, what you are trying to do is faceted histograms. Here's a full reprex with a very similar look that doesn't use ggridges
at all:
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软件包(v0.3.0)创建于2020-07-27
这篇关于向ggplot2 ggridges(joyplot)添加辅助轴以显示每个bin中的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!