gpt4 book ai didi

r - 使用 xyplot 进行三因子绘图

转载 作者:行者123 更新时间:2023-12-04 23:08:37 24 4
gpt4 key购买 nike

我遇到了无法解决的 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 等于第三个因子的级别数)。
所以,我的目标是:
  • 绘制第 3 个因子,并将该图分成 N 个子图,其中 N 是第 3 个因子的水平。
  • 最好作为函数工作,因为我需要绘制几个变量。
    下面是只有两个因子的示例图,以及我绘制 2 个因子的工作示例。

  • 先谢谢了~

    马可
    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 be !
    Using Deepayan's solution, I am able to add the error bar like this based on 2 factors

    最佳答案

    这是另一种方法,使用 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

    关于r - 使用 xyplot 进行三因子绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692428/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com