ggplot2:具有均值/95% 置信区间线的密度图
ggplot2: Density plot with mean / 95% confidence interval line
我知道有一种方法可以用箱线图绘制密度图,如下所示:所以基本上,在这个图中,使用了中位数和四分位数。
但是,我无法找到如何表达每个密度图的均值和置信区间。我想知道是否有一种方法可以基于 ggplot2 在 x-axis 上绘制 "mean & confidence interval" 行 (而不是带有中位数和四分位数的箱线图)。
我尝试使用 geom_errorbarh,但未能生成我想要查看的内容。
这里是 sum_stat.
中保存的均值和 95% 置信区间计算的 R 代码
library(ggplot2)
library(ggridges)
library(grid)
library(reshape2)
library(ggstance)
library(dplyr)
# Generating the dataset
x <- data.frame(v1=rnorm(5000, mean = -0.02, sd = 0.022),
v2=rnorm(5000, mean = 0.02, sd = 0.022),
v3=rnorm(5000, mean = 0.04, sd = 0.022))
colnames(x) <- c("A", "B", "C")
# Summary statistics
mean_vec <- colMeans(x)
sd_vec <- apply(x, 2, sd)
n <- nrow(x)
error <- qnorm(0.975)*sd_vec/sqrt(n)
left <- mean_vec - error
right <- mean_vec + error
sum_stat <- cbind(left, mean_vec, right)
# Melting the data
data <- melt(x)
# head(data); str(data)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.2, scale=0.8) +
geom_boxploth(aes(fill = variable), width = 0.06, outlier.shape = NA)
我期待听到你们的任何消息!
谢谢。
要使用 geom_errorbarh
,您必须通过 inherit.aes = FALSE
才能绘制均值和 CI。 (注意:我还在数据框中转换您的 sum_stat
并添加一列 variable
以使绘图更容易)
sum_stat <- data.frame(sum_stat)
sum_stat$variable = rownames(sum_stat)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.2, scale=0.8) +
geom_point(inherit.aes = FALSE, data = sum_stat,
aes(x= mean_vec, y = variable, color = variable),show.legend = FALSE)+
geom_errorbarh(inherit.aes = FALSE, data = sum_stat,
aes(xmin = left, xmax = right, y = variable, color = variable),
height = 0.1, show.legend = FALSE)
是您要找的吗?
我知道有一种方法可以用箱线图绘制密度图,如下所示:所以基本上,在这个图中,使用了中位数和四分位数。
但是,我无法找到如何表达每个密度图的均值和置信区间。我想知道是否有一种方法可以基于 ggplot2 在 x-axis 上绘制 "mean & confidence interval" 行 (而不是带有中位数和四分位数的箱线图)。
我尝试使用 geom_errorbarh,但未能生成我想要查看的内容。
这里是 sum_stat.
中保存的均值和 95% 置信区间计算的 R 代码library(ggplot2)
library(ggridges)
library(grid)
library(reshape2)
library(ggstance)
library(dplyr)
# Generating the dataset
x <- data.frame(v1=rnorm(5000, mean = -0.02, sd = 0.022),
v2=rnorm(5000, mean = 0.02, sd = 0.022),
v3=rnorm(5000, mean = 0.04, sd = 0.022))
colnames(x) <- c("A", "B", "C")
# Summary statistics
mean_vec <- colMeans(x)
sd_vec <- apply(x, 2, sd)
n <- nrow(x)
error <- qnorm(0.975)*sd_vec/sqrt(n)
left <- mean_vec - error
right <- mean_vec + error
sum_stat <- cbind(left, mean_vec, right)
# Melting the data
data <- melt(x)
# head(data); str(data)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.2, scale=0.8) +
geom_boxploth(aes(fill = variable), width = 0.06, outlier.shape = NA)
我期待听到你们的任何消息!
谢谢。
要使用 geom_errorbarh
,您必须通过 inherit.aes = FALSE
才能绘制均值和 CI。 (注意:我还在数据框中转换您的 sum_stat
并添加一列 variable
以使绘图更容易)
sum_stat <- data.frame(sum_stat)
sum_stat$variable = rownames(sum_stat)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.2, scale=0.8) +
geom_point(inherit.aes = FALSE, data = sum_stat,
aes(x= mean_vec, y = variable, color = variable),show.legend = FALSE)+
geom_errorbarh(inherit.aes = FALSE, data = sum_stat,
aes(xmin = left, xmax = right, y = variable, color = variable),
height = 0.1, show.legend = FALSE)
是您要找的吗?