将跨类别的总和映射到 ggplot2 中的美学
Mapping a sum across categories to an aesthetic in ggplot2
我的数据由多个样本、两个条件的组合以及这些条件的结果组成,在本例中为真阳性数和假阳性数。
我想出的最好的显示方法是堆积点图。这是单个样本的结果,基本上是我想要的结果:
现在,我想做的是对所有样本的真阳性和假阳性总数求和,并以完全相同的方式绘制它们。当我尝试时,每个样本的所有点都堆叠在一起,而不是相加并绘制在一起,如下所示:
(注意靶心图案,每个点应该只有 2 个圆圈。)
这是一些相同形式的较小样本数据,以及我尝试过的方法,使用 stat_sum():
require(dplyr)
samples <- c(rep("Sample 1", 4), rep("Sample 2", 4), rep("Sample 3", 4))
cond1 <- c("A", "A", "B", "B")
cond2 <- rep(c("X", "Y"))
data <- as.data.frame(cbind(samples, cond1, cond2))
data$true <- sample(30, length(data$samples))
data$false <- sample(20, length(data$samples))
data <- gather(data, type, hits, true, false)
#The good single-sample version
ggplot(filter(data, sample == "Sample 1"), aes(
x = cond1, y = cond2, size = hits, color = type)) +
geom_point(alpha = 0.2) +
scale_size_area(max_size = 20)
#Trying stat_sum() across hits
ggplot(data, aes(x = cond1, y = cond2, size = hits, color = type)) +
stat_sum(aes(group = hits), alpha =0.2) +
scale_size_area(max_size = 20)
#Trying stat_sum() weighting by hits
ggplot(data, aes(x = cond1, y = cond2, size = hits, color = type)) +
stat_sum(aes(group = 1, weight = hits), alpha =0.2) +
scale_size_area(max_size = 20)
我怎样才能得到样本中正确和错误命中的总和,并按条件绘制它们?
在使用 dplyr 的 group_by() 和 summarize() 工作之前转换数据集:
require(dplyr)
grouped_data <- group_by(data, cond1, cond2, type)
summarize(grouped_data, hits = sum(hits))
ggplot(grouped_data, aes(x = cond1, y = cond2, size = hits, color = type)) +
geom_point(alpha = 0.2) +
scale_size_area(max_size = 20)
我的数据由多个样本、两个条件的组合以及这些条件的结果组成,在本例中为真阳性数和假阳性数。
我想出的最好的显示方法是堆积点图。这是单个样本的结果,基本上是我想要的结果:
现在,我想做的是对所有样本的真阳性和假阳性总数求和,并以完全相同的方式绘制它们。当我尝试时,每个样本的所有点都堆叠在一起,而不是相加并绘制在一起,如下所示:
(注意靶心图案,每个点应该只有 2 个圆圈。)
这是一些相同形式的较小样本数据,以及我尝试过的方法,使用 stat_sum():
require(dplyr)
samples <- c(rep("Sample 1", 4), rep("Sample 2", 4), rep("Sample 3", 4))
cond1 <- c("A", "A", "B", "B")
cond2 <- rep(c("X", "Y"))
data <- as.data.frame(cbind(samples, cond1, cond2))
data$true <- sample(30, length(data$samples))
data$false <- sample(20, length(data$samples))
data <- gather(data, type, hits, true, false)
#The good single-sample version
ggplot(filter(data, sample == "Sample 1"), aes(
x = cond1, y = cond2, size = hits, color = type)) +
geom_point(alpha = 0.2) +
scale_size_area(max_size = 20)
#Trying stat_sum() across hits
ggplot(data, aes(x = cond1, y = cond2, size = hits, color = type)) +
stat_sum(aes(group = hits), alpha =0.2) +
scale_size_area(max_size = 20)
#Trying stat_sum() weighting by hits
ggplot(data, aes(x = cond1, y = cond2, size = hits, color = type)) +
stat_sum(aes(group = 1, weight = hits), alpha =0.2) +
scale_size_area(max_size = 20)
我怎样才能得到样本中正确和错误命中的总和,并按条件绘制它们?
在使用 dplyr 的 group_by() 和 summarize() 工作之前转换数据集:
require(dplyr)
grouped_data <- group_by(data, cond1, cond2, type)
summarize(grouped_data, hits = sum(hits))
ggplot(grouped_data, aes(x = cond1, y = cond2, size = hits, color = type)) +
geom_point(alpha = 0.2) +
scale_size_area(max_size = 20)