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
and, if I changed the geom into "bar", the output is OK.
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:
Plot the 3rd facotors, and split the plot into N subplots, where N is the levels of the 3rd factor.
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
)
!
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With