0

我正在使用 ggplot 从来自不同组的 4 周内的一组体重数据中绘制均值的分组条形图。我正在尝试绘制标准误差的误差条,但它们不是以每个条为中心,而是以每个集群为中心。见图片

我有脚本:

data <- read.table(file="groups.txt",head=TRUE,sep="\t")
attach(data)
melted <- melt(data, id.vars=c("id", "group"))
means <- ddply(melted, c("group", "variable"), summarise,
           mean=mean(value))
means.sem <- ddply(melted, c("group", "variable"), summarise,
               mean=mean(value), sem=sd(value)/sqrt(length(value)))
means.sem <- transform(means.sem, lower=mean-sem, upper=mean+sem)

m.bp<-ggplot(means, aes(variable, mean)) +   
  geom_bar(aes(fill = group), position = "dodge", stat="identity") + xlab("Age") + ylab("Weight(g)") + scale_x_discrete(labels=c("w1" = "P7", "w2" =  "P14",
                          "w3" = "P21", "w4" = "P28")) + scale_fill_manual(values = c("orange", "chocolate2", "grey32", "grey25"),name="Genotype\n/Sex", breaks=c("kf", "km", "wf", "wm"),
                     labels=c("Dlk1 KO Female", "Dlk1 KO Male", "WT Female", "WT Male"))

m.bp + geom_errorbar(aes(ymax=upper,ymin=lower), width=.2, position=position_dodge(0.9),
                          data=means.sem)

它可以很好地根据年龄对体重数据进行分组。但是,当我绘制它时,我的误差线集中在集群上,而不是单独在每个条上。有没有人知道如何让误差线放在正确的位置?

4

0 回答 0