ggridges 直方图(stat = binline)下的阴影区域如何?
How shade area under ggridges histogram (stat = binline)?
使用 Dr. Evers 使用 ggridges 对密度曲线下的区域进行阴影效果很好。然而,我发现密度曲线可能具有欺骗性,因为它们暗示数据存在而实际上不存在。因此,我想我会用普通直方图尝试这种着色技术。
但是,当我尝试将它与直方图一起使用时,阴影有点偏离。这是为什么?
library(tidyverse)
install.packages("ggridges", dependencies=TRUE) # there are many
library(ggridges)
t2 <- structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04",
"1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10",
"1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04",
"1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10",
"1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9,
18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8,
22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853",
"1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853",
"1853", "1854", "1854", "1854", "1854", "1854", "1854", "1854",
"1854", "1854", "1854", "1854", "1854")), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date",
"t", "year"))
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline", bins = 10, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges()
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)
确实发生了一些奇怪的事情。请参阅下面的 "conclusion"。
如果我们仅绘制 gg
:
gg;
如果我们绘制 gg
加上一个阶梯应该对应于 gg
:
gg +
geom_step(
data = d,
aes(xmax, ymax, group = group),
direction = "vh", col = "red", size = 2);
所以添加 geom_step
以某种方式改变了 gg
。我不明白怎么会这样。您可以看到 geom_step
(红色曲线)在单独绘制 gg
时确实对应于直方图的轨迹(参见第一个图)。
如果您愿意调整 bin 的大小并移动 bin 以使 bin 边界恰好位于您的分界线(此处为 10),则以下方法有效。
ggplot(t2, aes(x = t, y = year, fill = ifelse(..x..>=10, ">= 10", "< 10"))) +
geom_density_ridges_gradient(stat = "binline", binwidth = 3,
center = 8.5, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges() +
scale_fill_manual(values = c("gray70", "red"), name = NULL)
之所以会观察到你所做的效果,是因为x轴在第一张图和第二张图之间发生了变化,而x轴范围对bins的绘制方式有影响。有两种解决方案:您可以固定 x 轴范围或通过 center
和 binwidth
而不是 bins
定义 bin。 (无论如何,在我看来,无论您如何对待 x 轴,第二个选项总是首选。)
首先,固定x轴范围:
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline", bins = 10, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges() +
scale_x_continuous(limits = c(-12, 28)) # this is where the change is
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)
二、备选bin定义:
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline",
binwidth = 3, center = 8.5, # this is where the change is
scale = 0.8, draw_baseline = TRUE) +
theme_ridges()
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)
使用 Dr. Evers
但是,当我尝试将它与直方图一起使用时,阴影有点偏离。这是为什么?
library(tidyverse)
install.packages("ggridges", dependencies=TRUE) # there are many
library(ggridges)
t2 <- structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04",
"1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10",
"1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04",
"1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10",
"1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9,
18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8,
22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853",
"1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853",
"1853", "1854", "1854", "1854", "1854", "1854", "1854", "1854",
"1854", "1854", "1854", "1854", "1854")), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date",
"t", "year"))
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline", bins = 10, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges()
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)
确实发生了一些奇怪的事情。请参阅下面的 "conclusion"。
如果我们仅绘制
gg
:gg;
如果我们绘制
gg
加上一个阶梯应该对应于gg
:gg + geom_step( data = d, aes(xmax, ymax, group = group), direction = "vh", col = "red", size = 2);
所以添加 geom_step
以某种方式改变了 gg
。我不明白怎么会这样。您可以看到 geom_step
(红色曲线)在单独绘制 gg
时确实对应于直方图的轨迹(参见第一个图)。
如果您愿意调整 bin 的大小并移动 bin 以使 bin 边界恰好位于您的分界线(此处为 10),则以下方法有效。
ggplot(t2, aes(x = t, y = year, fill = ifelse(..x..>=10, ">= 10", "< 10"))) +
geom_density_ridges_gradient(stat = "binline", binwidth = 3,
center = 8.5, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges() +
scale_fill_manual(values = c("gray70", "red"), name = NULL)
之所以会观察到你所做的效果,是因为x轴在第一张图和第二张图之间发生了变化,而x轴范围对bins的绘制方式有影响。有两种解决方案:您可以固定 x 轴范围或通过 center
和 binwidth
而不是 bins
定义 bin。 (无论如何,在我看来,无论您如何对待 x 轴,第二个选项总是首选。)
首先,固定x轴范围:
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline", bins = 10, scale = 0.8,
draw_baseline = TRUE) +
theme_ridges() +
scale_x_continuous(limits = c(-12, 28)) # this is where the change is
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)
二、备选bin定义:
gg <- ggplot(t2, aes(x = t, y = year)) +
geom_density_ridges(stat = "binline",
binwidth = 3, center = 8.5, # this is where the change is
scale = 0.8, draw_baseline = TRUE) +
theme_ridges()
# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]
# Add geom_ribbon for shaded area
gg +
geom_ribbon(
data = transform(subset(d, x >= 10), year = group),
aes(x, ymin = ymin, ymax = ymax, group = group),
fill = "red",
alpha = 1.0)