在脊线图中的所有面板中添加阴影区域
Add shaded region across all panels in ridgeline plot
我有一个数据框df
:
set.seed(3467)
df<- data.frame(method= c(rep("A", 1000), rep("B", 1000), rep("C", 1000)),
beta=c(rnorm(1000, mean=0, sd=1),rnorm(1000, mean=2, sd=1.4),rnorm(1000, mean=0, sd=0.5)))
我希望像这样创建一个山脊图:
ggplot(df, aes(x = beta, y = method, color = method, fill = method)) +
geom_density_ridges(size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)
我想像这样添加一个阴影矩形:
ggplot(df) +
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "gray") +
geom_density_ridges2(aes(x = beta, fill = method, y = method))
然而,这次尝试没有成功:
ggplot(df, aes(x = beta, y = method, color = method, fill = method)) +
geom_density_ridges(size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)+
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "grey")
geom_rect()
函数似乎不适用于 geom_density_ridges()
,仅 geom_density_ridges2()
。
这是您要找的吗?这是对您的代码的轻微修改。我认为如果某些元素无法识别,我们可以为每一层具体指定数据框和aes
。
library(ggplot2)
library(ggridges)
ggplot() +
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "gray") +
geom_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)
我有一个数据框df
:
set.seed(3467)
df<- data.frame(method= c(rep("A", 1000), rep("B", 1000), rep("C", 1000)),
beta=c(rnorm(1000, mean=0, sd=1),rnorm(1000, mean=2, sd=1.4),rnorm(1000, mean=0, sd=0.5)))
我希望像这样创建一个山脊图:
ggplot(df, aes(x = beta, y = method, color = method, fill = method)) +
geom_density_ridges(size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)
我想像这样添加一个阴影矩形:
ggplot(df) +
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "gray") +
geom_density_ridges2(aes(x = beta, fill = method, y = method))
然而,这次尝试没有成功:
ggplot(df, aes(x = beta, y = method, color = method, fill = method)) +
geom_density_ridges(size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)+
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "grey")
geom_rect()
函数似乎不适用于 geom_density_ridges()
,仅 geom_density_ridges2()
。
这是您要找的吗?这是对您的代码的轻微修改。我认为如果某些元素无法识别,我们可以为每一层具体指定数据框和aes
。
library(ggplot2)
library(ggridges)
ggplot() +
geom_rect(data = data.frame(x = 1),
xmin = -1, xmax = 1, ymin = -Inf, ymax = Inf,
alpha = 0.5, fill = "gray") +
geom_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
size=1)+
scale_fill_manual(values = c("#228b2250", "#0072B250", "#483d8b50")) +
scale_color_manual(values = c("#228b22", "#0072B2", "#483d8b"), guide = "none") +
stat_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=1)