如何在 R 中用 ggplot2 叠加抖动点和平滑分布的均值和误差条?
How to overlay means and error bars with jitter dots and smooth distribution with ggplot2 in R?
为了获得数据集的完整图片,一种解决方案是显示均值以及均值周围的一些误差条以及各个分数的抖动点,最后是这些分数的平滑分布。一个例子是
taken from Yang, B. W., et al. (2021).
我们如何将点、误差条、抖动点和直方图全部叠加在同一个图中,每个之间有一个小间距?
为了说明的目的,假设数据是
x1=c(2.0,2.1,2.5,2.7,2.8,3.1)
x2=c(2.5,2.9,3.0,3.2,3.3,3.9)
x=data.frame(cbind(x1,x2))
用于绘制点和误差条的统计数据是
group = c(1, 2)
centr = c(2.53, 3.13)
width = c(0.50, 0.50)
stats = data.frame( cbind(group, centr, centr-width, centr+width ) )
我设法用
的点和误差线绘制了图
ggplot( stats ) +
geom_point( aes(x=group, y=centr, size = 1) ) +
geom_errorbar(stat="identity", position=position_dodge(.9), aes( x=group, ymin=V3, ymax=V4), width=0.1 ) +
scale_y_continuous("mean ratings")
抖动点
ggplot( x ) +
geom_jitter( aes( y= x1, x = 1, col=1), width=0.15 ) +
geom_jitter( aes( y= x2, x = 2, col=2), width=0.15 )
但我对平滑分布一无所知。
此外,如果我希望将两组数据分开(第一组的点,误差条,左边的抖动点和直方图,比如说,第二组的点,误差条,抖动点和直方图向右),需要进行哪些更改?
基本上你可以像这样达到你想要的结果:
- 将您的 x 数据集转换为长格式
- 添加密度开关 x 和 y 并使用
coord_flip
代替
- 定位误差线和抖动点设置
y=-2/-1
- 为了得到你想要的数据组分开的图,你可以按组分面,但删除
panel.spacing
和 strip.text
x1 <- c(2.0, 2.1, 2.5, 2.7, 2.8, 3.1)
x2 <- c(2.5, 2.9, 3.0, 3.2, 3.3, 3.9)
x <- data.frame(cbind(x1, x2))
x_long <- tidyr::pivot_longer(x, everything(), names_prefix = "x", names_to = "group")
x_long$group <- as.integer(x_long$group)
group <- c(1, 2)
centr <- c(2.53, 3.13)
width <- c(0.50, 0.50)
stats <- data.frame(cbind(group, centr, centr - width, centr + width))
library(ggplot2)
ggplot(stats, aes(color = factor(group))) +
geom_point(aes(y = -2, x = centr), size = 1) +
geom_errorbar(stat = "identity", aes(y = -2, xmin = V3, xmax = V4), width = 0.1) +
geom_jitter(data = x_long, aes(x = value, y = -1), width = 0.1) +
geom_density(data = x_long, aes(x = value, fill = factor(group), group = group), alpha = .7) +
scale_x_continuous("mean ratings") +
scale_y_continuous(expand = c(0, .2)) +
coord_flip() +
facet_wrap(~group) +
theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.ticks.x = element_blank(),
panel.spacing.x = unit(0, "pt"),
strip.text.x = element_blank()) +
labs(color = NULL, fill = NULL)
为了获得数据集的完整图片,一种解决方案是显示均值以及均值周围的一些误差条以及各个分数的抖动点,最后是这些分数的平滑分布。一个例子是
我们如何将点、误差条、抖动点和直方图全部叠加在同一个图中,每个之间有一个小间距?
为了说明的目的,假设数据是
x1=c(2.0,2.1,2.5,2.7,2.8,3.1)
x2=c(2.5,2.9,3.0,3.2,3.3,3.9)
x=data.frame(cbind(x1,x2))
用于绘制点和误差条的统计数据是
group = c(1, 2)
centr = c(2.53, 3.13)
width = c(0.50, 0.50)
stats = data.frame( cbind(group, centr, centr-width, centr+width ) )
我设法用
的点和误差线绘制了图ggplot( stats ) +
geom_point( aes(x=group, y=centr, size = 1) ) +
geom_errorbar(stat="identity", position=position_dodge(.9), aes( x=group, ymin=V3, ymax=V4), width=0.1 ) +
scale_y_continuous("mean ratings")
抖动点
ggplot( x ) +
geom_jitter( aes( y= x1, x = 1, col=1), width=0.15 ) +
geom_jitter( aes( y= x2, x = 2, col=2), width=0.15 )
但我对平滑分布一无所知。
此外,如果我希望将两组数据分开(第一组的点,误差条,左边的抖动点和直方图,比如说,第二组的点,误差条,抖动点和直方图向右),需要进行哪些更改?
基本上你可以像这样达到你想要的结果:
- 将您的 x 数据集转换为长格式
- 添加密度开关 x 和 y 并使用
coord_flip
代替 - 定位误差线和抖动点设置
y=-2/-1
- 为了得到你想要的数据组分开的图,你可以按组分面,但删除
panel.spacing
和strip.text
x1 <- c(2.0, 2.1, 2.5, 2.7, 2.8, 3.1)
x2 <- c(2.5, 2.9, 3.0, 3.2, 3.3, 3.9)
x <- data.frame(cbind(x1, x2))
x_long <- tidyr::pivot_longer(x, everything(), names_prefix = "x", names_to = "group")
x_long$group <- as.integer(x_long$group)
group <- c(1, 2)
centr <- c(2.53, 3.13)
width <- c(0.50, 0.50)
stats <- data.frame(cbind(group, centr, centr - width, centr + width))
library(ggplot2)
ggplot(stats, aes(color = factor(group))) +
geom_point(aes(y = -2, x = centr), size = 1) +
geom_errorbar(stat = "identity", aes(y = -2, xmin = V3, xmax = V4), width = 0.1) +
geom_jitter(data = x_long, aes(x = value, y = -1), width = 0.1) +
geom_density(data = x_long, aes(x = value, fill = factor(group), group = group), alpha = .7) +
scale_x_continuous("mean ratings") +
scale_y_continuous(expand = c(0, .2)) +
coord_flip() +
facet_wrap(~group) +
theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.ticks.x = element_blank(),
panel.spacing.x = unit(0, "pt"),
strip.text.x = element_blank()) +
labs(color = NULL, fill = NULL)