[R] Specifying strip names in xyplot (strip.custom)
Duncan Mackay
dulcalma at bigpond.com
Sat Jan 17 14:12:08 CET 2015
Hi Ahmed
Without a dataset it is harder to know what is exactly going on
How does the following look
library(latticeExtra)
useOuterStrips(strip = strip.custom(factor.levels = top factor levels
par.strip.text = list(cex = 0.9)),
strip.left = strip.custom(factor.levels = left factor
levels,
par.strip.text = par.strip.text =
list(cex= 0.9)),
xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
layout=c(5,3,1),
xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
ylab=expression(bold(paste("Grain yield (Mg ",ha^-1,")"))),
ylim=c(10,20),
scales = list(alternating=F,
y=list(at=c(10,12,14,16,18,20,22)),font=2,
cex=0.7, col="black"),aspect = 1,
panel = function(x,y,subscripts,groups,...) {
#panel.grid(-1,-1)
m1 <- lm(y ~ x)
x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
pred <- predict(m1, x.seq, se.fit=T)
grid.polygon(c(x.seq$x,rev(x.seq$x)),
c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
"native")
llines(x.seq$x, pred$fit, lty = 1)
panel.xyplot(x,y)
print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[ 1,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
print(summary(m1))
}
)
) ## useOuterStrips
You need to fill the strips in the order of the panels so you will have to
put a string of factor names that correspond to the strips on each of the
panels. It makes it easier with useOuterStrips
Have a look at strip.default
xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
layout=c(5,3,1),
xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
ylab=expression(bold(paste("Grain yield (Mg ",ha^-1,")"))),
ylim=c(10,20),
strip=strip.custom(interaction(S,Loc),style=2, # Guess but something
along these lines
par.strip.text = list(cex = 0.9)),
scales =
list(alternating=F,y=list(at=c(10,12,14,16,18,20,22)),font=2, cex=0.7,
col="black"),aspect = 1,
panel = function(x,y,subscripts,groups,...) {
#panel.grid(-1,-1)
m1 <- lm(y ~ x)
x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
pred <- predict(m1, x.seq, se.fit=T)
grid.polygon(c(x.seq$x,rev(x.seq$x)),
c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
"native")
llines(x.seq$x, pred$fit, lty = 1)
panel.xyplot(x,y)
print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[ 1,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
print(summary(m1))
}
)
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
From: Ahmed Attia [mailto:ahmedatia80 at gmail.com]
Sent: Saturday, 17 January 2015 06:52
To: r-help
Cc: Duncan Mackay
Subject: Specifying strip names in xyplot (strip.custom)
Hi R Users,
I would like to specify strip names in xyplot. I have 5 sources (S) and 3
locations (Loc). How I can force R to specifiy the strip names for each
source and location.
Below is my code;
S*Loc represents source by location. I do not know how to specify which
panel or factors.levels. In the code below, factor.levels represent three
Loc
Thanks for your help.
Ahmed
trellis.device(col=F)
xyplot(GrainM~Nrate|S*Loc, data = Ahmed,
layout=c(5,3,1),
xlab=expression(bold(paste("N rate (kg ", ha^-1,")"))),
ylab=expression(bold(paste("Grain yield (Mg ",ha^-1,")"))),
ylim=c(10,20),
strip=strip.custom(which.panel=3,factor.levels=c("Pierce","HAL","Concord"),s
trip.names=c(TRUE),strip.levels=c(TRUE),style=2,
par.strip.text = list(cex = 0.9)),
scales =
list(alternating=F,y=list(at=c(10,12,14,16,18,20,22)),font=2, cex=0.7,
col="black"),aspect = 1,
panel = function(x,y,subscripts,groups,...) {
#panel.grid(-1,-1)
m1 <- lm(y ~ x)
x.seq <- data.frame(x=seq(min(x)*0.95,max(x)*1.05,max(x)/10))
pred <- predict(m1, x.seq, se.fit=T)
grid.polygon(c(x.seq$x,rev(x.seq$x)),
c(pred$fit + 2*pred$se.fit, rev(pred$fit - 2*pred$se.fit) ),
gp=gpar(col=0, fill="light grey", alpha = 1), default.units =
"native")
llines(x.seq$x, pred$fit, lty = 1)
panel.xyplot(x,y)
print(round(summary(m1)$coef[ 2,c(1,4) ],3) )
ltext(50,18.4,paste("Slope = ",round(summary(m1)$coef[ 2,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
ltext(50,19.5,paste("Intercept = ",round(summary(m1)$coef[ 1,c(1,4)
],3),sep=""),
cex=0.65, adj=0)
print(summary(m1))
}
)
Ahmed Attia, Ph.D.
Agronomist & Soil Scientist
Post-Doc Research Associate
Texas A&M AgriLife Research-Vernon
ahmed.attia at ag.tamu.edu
Cell phone: 001-979-248-5215
[[alternative HTML version deleted]]
More information about the R-help
mailing list