使用单独的数据框在ggplot中生成错误栏

时间:2014-10-24 03:20:17

标签: r ggplot2

我有一个如下所示的数据框:

 genotype     DIV3     DIV4 ...
 WT           12.4     15.2
 WT           35.4     35.3
 HET          1.3      1.2
 HET          1.5      5.2

我通过以下函数计算平均值和标准差:

  means = aggregate(. ~ genotype, data=dat, FUN=mean)
  errors = aggregate(. ~ genotype, data=dat, FUN=sd)

我正在使用ggplot2将方法绘制为散点图。我想将错误数据框用于错误栏,但由于我有两个数据帧,因此无法计算ymin和ymax。

有更好的方法吗?

编辑: ggplot2代码:

 x = melt(means)
 ggplot(x_melt, aes(group=genotype, variable, value, col=genotype, shape = genotype)) + 
   geom_line() +
   geom_point(size=3)+
   theme(axis.text=element_text(size=14),
         axis.title.x=element_blank(),
         axis.text.x=element_text(angle = 45, vjust = 0.8, hjust = .9, color = "black"),
         axis.text.y=element_text(color="black"))

1 个答案:

答案 0 :(得分:1)

您可以通过在aggregate步骤中创建单个数据集,然后在绘制之前创建reshape来完成此操作。

dat2 <- do.call(`data.frame`,
         aggregate(. ~genotype, dat, FUN= function(x) c(Mean=mean(x), SD=sd(x))))


nm1 <- unique(gsub("\\..*", "", colnames(dat2)[-1]))
datN <- reshape(dat2, direction="long", idvar="genotype", 
                       varying=list(c(2,4), c(3,5)),sep=".")

datN$time <- nm1[datN$time]
colnames(datN)[3:4] <- c("Mean", "SD")

library(ggplot2)

ggplot(datN, aes(group=genotype, time, Mean, col=genotype,
    shape=genotype))+
    geom_line()+
    geom_point(size=3)+
    geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.1)+
     theme(axis.text=element_text(size=14),
     axis.title.x=element_blank(),
     axis.text.x=element_text(angle = 45, vjust = 0.8, hjust = .9, color = "black"),
     axis.text.y=element_text(color="black"))

或者您可以merge融化的数据集meanserrors

   library(reshape2)
   x_melt <- melt(means, value.name="Mean")
   y_melt <- melt(errors, value.name="SD")

  datN1 <- merge(x_melt, y_melt)

  ggplot(datN1, aes(group=genotype, variable, Mean, col=genotype,
     shape=genotype))+
     geom_line()+
     geom_point(size=3)+
     geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.1)+
     theme(axis.text=element_text(size=14),
       axis.title.x=element_blank(),
       axis.text.x=element_text(angle = 45, vjust = 0.8, hjust = .9, color = "black"),
       axis.text.y=element_text(color="black"))

数据

 dat <- structure(list(genotype = c("WT", "WT", "HET", "HET"), DIV3 = c(12.4, 
 35.4, 1.3, 1.5), DIV4 = c(15.2, 35.3, 1.2, 5.2)), .Names = c("genotype", 
 "DIV3", "DIV4"), class = "data.frame", row.names = c(NA, -4L))