使用 xyplot 绘制三因素图

发布于 2024-11-01 20:37:07 字数 3419 浏览 1 评论 0 原文

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

> 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 

ggplot 的输出 而且,如果我将 geom 更改为“bar”,则输出正常。 Geom= 时的 ggplot 输出">

below is the background for this Q
#

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

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

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

预先感谢~

Marco

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 
        )

这是我希望 mydata 成为的数字使用 Deepayan 的解决方案,我可以根据两个因素添加这样的误差线

I had a problem with ggplot that I am not able to solve, so maybe someone here can point out the reason. Sorry that I am not able to upload my dataset, but some data description can be found below. The output of the ggplot is shown below, except NO line, every other thing is OK.

> 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
and, if I changed the geom into "bar", the output is OK.
The ggplot output when geom="bar"

below is the background for this Q

#

I would like to plot several variables that were subjected to the same, 3 factors. Using xyplot, I am able to plot 2 of them, within one figure. However, I have no idea how to include the third, and arrange the figure into N subplots (N equals the level number of the third factor).
So, my aims would be:

  1. Plot the 3rd facotors, and split the plot into N subplots, where N is the levels of the 3rd factor.

  2. Better to work as a function, as I need to plot a several variables.
    Below is the example figure with only two factors, and my working example to plot 2 factors.

Thanks in advance~

Marco

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-11-08 20:37:07

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

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

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)

在此处输入图像描述

您可以更进一步,生成二维面:

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)

在此处输入图像描述

Here is another way of doing it, using the magic of ggplot. Because ggplot will calculate summaries for you, I suspect it means you can skip the entire step of doing aov.

The key is that your data should be in single data.frame that you can pass to ggplot. Note that I have created new sample data to demonstrate.

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

You can go one step further and produce facets in two dimensions:

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

末骤雨初歇 2024-11-08 20:37:07

这已经非常接近了,但我忘记了如何使用 Lattice 和 Deepayan 书中的 group 变量为错误线着色。

## 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

This gets pretty close, but I forget how to colour the error lines using the group variable in Lattice and Deepayan's book is at work.

## 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, ...)
       })

And produces:

xyplot with error bars

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文