使用xyplot进行三因素绘图

时间:2011-04-17 08:22:21

标签: r plot ggplot2

我遇到了ggplot的问题我无法解决,所以也许有人可以指出原因。很抱歉,我无法上传我的数据集,但可以在下面找到一些数据描述。 ggplot的输出如下所示,除NO行外,其他所有内容都可以。

> all.data<-read.table("D:/PAM/data/Rural_Recovery_Edit.csv",head=T,sep=",")
> all.data$Water<-factor(all.data$Water,labels=c("W30","W60","W90"))
> all.data$Polymer<-factor(all.data$Polymer,labels=c("PAM-0  ","PAM-10  ","PAM-40  "))
> all.data$Group<-factor(all.data$Group,labels=c("Day20","Day25","Day30"))
> dat<-data.frame(Waterconsump=all.data[,9],Water=all.data$Water,Polymer=all.data$Polymer,Age=all.data$Group)

> ggplot(dat,aes(x=Water,y=Waterconsump,colour=Polymer))+
+ stat_summary(fun.y=mean, geom="line",size=2)+
+ stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar")+#,position="dodge"
+ facet_grid(~Age)

> dim(dat)
[1] 108   4
> head(dat)
  Waterconsump Water  Polymer   Age
1         10.5   W30 PAM-10   Day20
2         10.3   W30 PAM-10   Day20
3         10.1   W30 PAM-10   Day20
4          7.7   W30 PAM-10   Day20
5          8.6   W60 PAM-10   Day20
6          8.4   W60 PAM-10   Day20
> table(dat$Water)

W30 W60 W90 
 36  36  36 
> table(dat$Polymer)

 PAM-0   PAM-10   PAM-40   
      36       36       36 
> table(dat$Age)

Day20 Day25 Day30 
   36    36    36 

The out put of the ggplot 并且,如果我将geom更改为“bar”,则输出正常。 The ggplot output when geom="bar"

below is the background for this Q

我想绘制几个受到相同3个因素影响的变量。使用xyplot,我可以在一个图中绘制其中的2个。但是,我不知道如何包含第三个,并将数字排列成N个子图(N等于第三个因子的级别数)。 所以,我的目标是:

  1. 绘制第3个facotors,并将绘图分成N个子图,其中N是第3个因子的水平。

  2. 最好是作为一个函数工作,因为我需要绘制几个变量。 下面是仅有两个因素的示例图,以及绘制2个因子的工作示例。

  3. 提前致谢〜

    library(reshape)
    library(agricolae)
    library(lattice)
    yr<-gl(10,3,90:99)
    trt<-gl(4,75,labels=c("A","B","C","D"))
    
    third<-gl(3,100,lables=c("T","P","Q")) ### The third factor to split the figure in to 4 subplots
    
    dat<-cbind(runif(300),runif(300,min=1,max=10),runif(300,min=100,max=200),runif(300,min=1000,max=1500))
    colnames(dat)<-paste("Item",1:4,sep="-")
    fac<-factor(paste(trt,yr,sep="-"))
    dataov<-aov(dat[,1]~fac)
    dathsd<-sort_df(HSD.test(dataov,'fac'),'trt')
    trtplt<-gl(3,10,30,labels=c("A","B","C"))
    yrplt<-factor(substr(dathsd$trt,3,4))
    
    prepanel.ci <- function(x, y, ly, uy, subscripts, ...) 
    { 
        x <- as.numeric(x) 
        ly <- as.numeric(ly[subscripts]) 
        uy <- as.numeric(uy[subscripts]) 
        list(ylim = range(y, uy, ly, finite = TRUE)) 
    } 
    panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) 
    { 
        x <- as.numeric(x) 
        y <- as.numeric(y) 
        ly <- as.numeric(ly[subscripts]) 
        uy <- as.numeric(uy[subscripts]) 
        panel.arrows(x, ly, x, uy, col = "black", 
                     length = 0.25, unit = "native", 
                     angle = 90, code = 3) 
        panel.xyplot(x, y, pch = pch, ...) 
    } 
    
    xyplot(dathsd$means~yrplt,group=trtplt,type=list("l","p"),
            ly=dathsd$means-dathsd$std.err,
            uy=dathsd$means+dathsd$std.err,
            prepanel = prepanel.ci, 
            panel = panel.superpose, 
            panel.groups = panel.ci 
            )
    

    This is the figure I would like mydata to beUsing Deepayan's solution, I am able to add the error bar like this based on 2 factors

2 个答案:

答案 0 :(得分:7)

这是另一种方法,使用ggplot的魔力。因为ggplot会为你计算摘要,我怀疑这意味着你可以跳过执行aov的整个步骤。

关键是您的数据应该是单data.frame个,您可以传递给ggplot。请注意,我已创建了新的示例数据以进行演示。

library(ggplot2)

df <- data.frame(
  value = runif(300),
  yr = rep(1:10, each=3),
  trt = rep(LETTERS[1:4], each=75),
  third = rep(c("T", "P", "Q"), each=100)
)

ggplot(df, aes(x=yr, y=value, colour=trt)) + 
  stat_summary(fun.y=mean, geom="line", size=2) +
  stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar") +
  facet_grid(~third)

enter image description here

您可以更进一步,在两个方面制作构面:

ggplot(df, aes(x=yr, y=value, colour=trt)) + 
  stat_summary(fun.y=mean, geom="line", size=2) +
  stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar") +
  facet_grid(trt~third)

enter image description here

答案 1 :(得分:2)

这非常接近,但我忘记了如何使用莱迪思中的group变量为错误行着色,而Deepayan的书正在起作用。

## format a new data structure with all variables we want
dat <- data.frame(dathsd[, c(2,5)], treat = trtplt, yrplt = yrplt,
                  upr = dathsd$means + 2 * dathsd$std.err,
                  lwr = dathsd$means - 2 * dathsd$std.err)
## compute ylims
ylims <- range(dat$lwr, dat$upr)
ylims <- ylims + (c(-1,1) * (0.05 * diff(ylims)))
## plot
xyplot(means ~ yrplt, data = dat, group = treat, lwr = dat$lwr, upr = dat$upr,
       type = c("p","l"), ylim = ylims,
       panel = function(x, y, lwr, upr, ...) {
           panel.arrows(x0 = x, y0 = lwr, x1 = x, y1 = upr,
                        angle = 90, code = 3, length = 0.05)
           panel.xyplot(x, y, ...)
       })

并产生:

xyplot with error bars