如何将平均线添加到 R 中的分组密度图?
How to add the mean line to grouped density plot in R?
我正在尝试绘制分组密度图并添加每个图的均值线;这是代码
data <- data.frame(
Accuracy=abs(rnorm(140)),
Species=c(rep("A.All",20),rep("B. double",60),rep("C.single",60),
rep("D.All",20),rep("E.double",60),rep("F.single",60)),
Modality=c(rep("All,w0",10),rep("double1,w0",10),rep("double2,w0",10),rep("double3,w0",10),
rep("single1,w0",10),rep("single2,w0",10),rep("single3,w0",10),
rep("All,w2",10),rep("double1,w2",10),rep("double2,w2",10),rep("double3,w2",10),
rep("single1,w2",10),rep("single2,w2",10),rep("single3,w2",10))
)
p<-ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density")
library(plyr)
mu <- ddply(data, "Modality", summarise, grp.mean=mean(Accuracy))
head(mu)
# Add mean lines
a<-p+geom_vline(data=mu, aes(xintercept=grp.mean, color=Modality),
linetype="dashed")+ xlab("Accuracy") + ylab("Density")
但是,根据输出图为
平均线绝对不正确,例如对于左上角的第一张图片,两个密度图应该有两条线,但是创建了几条线并为所有数字重复。
您可以同时指定 Species
和 Modality
plyr
dummy <- ddply(data, c("Species","Modality"), summarise, grp.mean=mean(Accuracy))
ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density") +
geom_vline(data = dummy, aes(xintercept = grp.mean, color = Modality))
dplyr
library(dplyr)
dummy <- data %>%
group_by(Species, Modality) %>%
summarize(mean = mean(Accuracy))
ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density") +
geom_vline(data = dummy, aes(xintercept = mean, color = Modality))
我正在尝试绘制分组密度图并添加每个图的均值线;这是代码
data <- data.frame(
Accuracy=abs(rnorm(140)),
Species=c(rep("A.All",20),rep("B. double",60),rep("C.single",60),
rep("D.All",20),rep("E.double",60),rep("F.single",60)),
Modality=c(rep("All,w0",10),rep("double1,w0",10),rep("double2,w0",10),rep("double3,w0",10),
rep("single1,w0",10),rep("single2,w0",10),rep("single3,w0",10),
rep("All,w2",10),rep("double1,w2",10),rep("double2,w2",10),rep("double3,w2",10),
rep("single1,w2",10),rep("single2,w2",10),rep("single3,w2",10))
)
p<-ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density")
library(plyr)
mu <- ddply(data, "Modality", summarise, grp.mean=mean(Accuracy))
head(mu)
# Add mean lines
a<-p+geom_vline(data=mu, aes(xintercept=grp.mean, color=Modality),
linetype="dashed")+ xlab("Accuracy") + ylab("Density")
但是,根据输出图为
平均线绝对不正确,例如对于左上角的第一张图片,两个密度图应该有两条线,但是创建了几条线并为所有数字重复。
您可以同时指定 Species
和 Modality
plyr
dummy <- ddply(data, c("Species","Modality"), summarise, grp.mean=mean(Accuracy))
ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density") +
geom_vline(data = dummy, aes(xintercept = grp.mean, color = Modality))
dplyr
library(dplyr)
dummy <- data %>%
group_by(Species, Modality) %>%
summarize(mean = mean(Accuracy))
ggplot(data, aes(x=Accuracy, fill=Modality)) +
geom_density(alpha=0.4)+
facet_wrap(. ~ Species) +
xlab("Accuracy") + ylab("Density") +
geom_vline(data = dummy, aes(xintercept = mean, color = Modality))