如何在ggplot2 facet_grid中安排面板

时间:2014-02-20 17:27:28

标签: r ggplot2 facet

我正在使用R

中的这个数据集

https://www.dropbox.com/s/g28etd2wywuu7on/Measurements.csv

我已完成以下数据操作

`dat<-read.csv("Measurements.csv")`

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                  conf.interval=.95, .drop=TRUE) 

{require(plyr)

  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }

  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )

  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))

  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult

 return(datac)
}

    summary.total<-summarySE(dat, measurevar="length", groupvars=c("Genotype",  "Concentration"))
pd <- position_dodge(.1)

问题

然后,我想根据Bgrnd列中的标签绘制数据,并按行分面。 使用ggplot2我已经完成了这个,但收到一条错误信息。我无法弄清楚如何让它发挥作用。

ggplot(summary.total, aes(x=Concentration, y=length, colour=Genotype, group=Genotype)) + 
  geom_errorbar(aes(ymin=length-se, ymax=length+se), colour="black", width=.3,     position=pd) +
  geom_line(position=pd) +
  geom_point(position=pd, size=3) +
  facet_grid(Bgrnd ~ .) +
  ylab("leaf segment width (mm)") +
  xlab("[Br]") +
  theme(axis.title = element_text(size=14,face="bold"), axis.text = element_text(size=13))

0 个答案:

没有答案