使用 ggplot2 在分离的小提琴图上叠加散点
Overlay scatterpoints on split violin plot with ggplot2
我按照 在 2x2 设计中生成分割小提琴图的好答案
现在假设这些数据来自不同受试者的重复测量。此外,我想在散点图中绘制单个数据(我知道该图可能最终会太忙,我想先看看)。
我快完成了,但是有一个小错误可能很容易修复。我包括一个完整的工作示例,以防有更好的方法来做到这一点。
这第一部分是我直接从上一个问题复制过来的:
library(dplyr)
library(ggplot2)
set.seed(20160229)
我将 subj
添加到我的数据框中,因为我想绘制每个对象的平均值
my_data = data.frame(
y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)),
x=c(rep('a', 2000), rep('b', 2000)),
m=c(rep('i', 1000), rep('j', 2000), rep('i', 1000)),
subj=c(rep(c(rep('1',200),rep('2',200),rep('3',200),rep('4',200),rep('5',200)),4))
)
pdat <- my_data %>%
group_by(x, m) %>%
do(data.frame(loc = density(.$y)$x,
dens = density(.$y)$y))
pdat$dens <- ifelse(pdat$m == 'i', pdat$dens * -1, pdat$dens)
pdat$dens <- ifelse(pdat$x == 'b', pdat$dens + 1, pdat$dens)
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal() +
theme(axis.title.x = element_blank())
到目前为止,效果很好。现在我尝试为每个主题添加我的平均值
meanY = aggregate(y ~ x + m + subj, my_data, mean, drop=TRUE)
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
geom_point(data=meanY, aes(fill = m, group = interaction(m, x))) +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal()
我收到错误:Error in eval(expr, envir, enclos) : object 'dens' not found
如果我理解正确,您需要在 geom_point
中指定 x
和 y
:
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal() +
theme(axis.title.x = element_blank()) +
geom_point(data = meanY, aes(x = ifelse(x == "a", 0, 1), y = y, fill = m, group = interaction(m, x)), shape = 21, colour = "black", show.legend = FALSE)
我按照
现在假设这些数据来自不同受试者的重复测量。此外,我想在散点图中绘制单个数据(我知道该图可能最终会太忙,我想先看看)。
我快完成了,但是有一个小错误可能很容易修复。我包括一个完整的工作示例,以防有更好的方法来做到这一点。
这第一部分是我直接从上一个问题复制过来的:
library(dplyr)
library(ggplot2)
set.seed(20160229)
我将 subj
添加到我的数据框中,因为我想绘制每个对象的平均值
my_data = data.frame(
y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)),
x=c(rep('a', 2000), rep('b', 2000)),
m=c(rep('i', 1000), rep('j', 2000), rep('i', 1000)),
subj=c(rep(c(rep('1',200),rep('2',200),rep('3',200),rep('4',200),rep('5',200)),4))
)
pdat <- my_data %>%
group_by(x, m) %>%
do(data.frame(loc = density(.$y)$x,
dens = density(.$y)$y))
pdat$dens <- ifelse(pdat$m == 'i', pdat$dens * -1, pdat$dens)
pdat$dens <- ifelse(pdat$x == 'b', pdat$dens + 1, pdat$dens)
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal() +
theme(axis.title.x = element_blank())
到目前为止,效果很好。现在我尝试为每个主题添加我的平均值
meanY = aggregate(y ~ x + m + subj, my_data, mean, drop=TRUE)
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
geom_point(data=meanY, aes(fill = m, group = interaction(m, x))) +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal()
我收到错误:Error in eval(expr, envir, enclos) : object 'dens' not found
如果我理解正确,您需要在 geom_point
中指定 x
和 y
:
ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) +
geom_polygon() +
scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
ylab('density') +
theme_minimal() +
theme(axis.title.x = element_blank()) +
geom_point(data = meanY, aes(x = ifelse(x == "a", 0, 1), y = y, fill = m, group = interaction(m, x)), shape = 21, colour = "black", show.legend = FALSE)