[R] plot auto key and text into panels using lattice
Duncan Mackay
dulcalma at bigpond.com
Tue Jul 21 14:34:19 CEST 2015
Forgot to send to list
Plus forgot to say that I appear to have no problems with script below
Hi Luigi
It is helpful to use a MWE - you had several errors in the script.
try these (adapting fig 5.11 from Deepayan Sarkar's book
data(SeatacWeather, package = "latticeExtra")
str(SeatacWeather)
MIN.X= 1
MAX.X = 31
MIN.Y = 15
MAX.Y = 70
LABELS <- paste("Well", 1:3)
TEXT_FOR_AUTOKEY <- paste("Label", 1:3)
xyplot(min.temp + max.temp ~ day | month, data = SeatacWeather,
ylab = "Temperature and Rainfall",
as.table = TRUE,
type = "l",
lty = 1,
xlab="X",
main="TITLE",
scales = list(
x = list(draw = FALSE),
y = list(draw = FALSE),
relation="same",
alternating=TRUE),
layout = c(3,1),
par.settings = list(
strip.background=list(col="white"),
axis.text = list(cex = 0.6),
par.xlab.text = list(cex = 0.75),
par.ylab.text = list(cex = 0.75),
par.main.text = list(cex = 0.8),
superpose.symbol = list(pch = ".", cex = 1)
),
strip = FALSE,
col = 3,
panel = function(x, y,...) {
panel.xyplot(x,y,...)
pnl = panel.number()
panel.text(MIN.X+(0.1*MAX.X),
MAX.Y-(0.1*MAX.Y),
labels=LABELS[panel.number()],
cex=0.3
)
}
)
xyplot(min.temp + max.temp ~ day | month, data = SeatacWeather,
ylab = "Temperature and Rainfall",
as.table = TRUE,
type = "l",
lty = 1,
xlab="X",
main="TITLE",
scales = list(
x = list(draw = FALSE),
y = list(draw = FALSE),
relation="same",
alternating=TRUE),
layout = c(3,1),
# auto.key= list(space = "centre"),
par.settings = list(
strip.background=list(col="white"),
axis.text = list(cex = 0.6),
par.xlab.text = list(cex = 0.75),
par.ylab.text = list(cex = 0.75),
par.main.text = list(cex = 0.8),
superpose.symbol = list(pch = ".", cex = 1)
),
strip = FALSE,
col = c(4,2),
key = list(
text=list(TEXT_FOR_AUTOKEY, col="black"),
space="top", columns=2,
lines=list(col=c(4,2))
## panel = panel.superpose #wrong place ; duplicated panel
function anyway
),
panel = function(x, y,...) {
panel.xyplot(x,y,...)
panel.text(MIN.X+(0.1*MAX.X),
MAX.Y-(0.1*MAX.Y),
labels=LABELS[panel.number()],
cex=0.3,
panel = panel.superpose
)
}
)
You cannot have key and auto.key
If you want to have 2 keys/ legends
see ?draw.key and legend argument lattice::xyplot
https://stat.ethz.ch/pipermail/r-help/2005-February/066264.html
an easier way for text is
library(grid)
xyplot(min.temp + max.temp ~ day | month, data = SeatacWeather,
ylab = "Temperature and Rainfall",
as.table = TRUE,
type = "l",
lty = 1,
xlab="X",
main="TITLE",
scales = list(
x = list(draw = FALSE),
y = list(draw = FALSE),
relation="same",
alternating=TRUE),
layout = c(3,1),
par.settings = list(
strip.background=list(col="white"),
axis.text = list(cex = 0.6),
par.xlab.text = list(cex = 0.75),
par.ylab.text = list(cex = 0.75),
par.main.text = list(cex = 0.8),
superpose.symbol = list(pch = ".", cex = 1)
),
strip = FALSE,
col = 3,
panel = function(x, y,...) {
panel.xyplot(x,y,...)
pnl = panel.number()
# possibly needs some fine tuning
grid.text(label=LABELS[panel.number()], x = unit(0.1,
"npc"), y = unit(0.9, "npc"), gp = gpar(cex = 0.3))
}
)
panel.number() used on its own can play up sometimes
better to save the value and use it.
If you are having problems start from the beginning with a minimal script eg
xyplot(min.temp + max.temp ~ day, data = SeatacWeather,
as.table = T,
layout = ...,
groups = ...)
and build up from there. Its amazing what you find out.
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi
Marongiu
Sent: Tuesday, 21 July 2015 08:39
To: r-help
Subject: [R] plot auto key and text into panels using lattice
Dear all,
I am writing some text into several panels which I can do with this
script (in capital the variables):
xyplot(Y ~ X | Z,
data = DATAFRAME,
groups = Z,
ylab= "Y",
xlab="X",
main="TITLE",
scales = list(
x = list(draw = FALSE),
y = list(draw = FALSE),
relation="same",
alternating=TRUE),
as.table = TRUE,
layout = LAYOUT,
par.settings = list(
strip.background=list(col="white"),
axis.text = list(cex = 0.6),
par.xlab.text = list(cex = 0.75),
par.ylab.text = list(cex = 0.75),
par.main.text = list(cex = 0.8),
superpose.symbol = list(pch = ".", cex = 1)
),
strip = FALSE,
type = "l",
col = 3,
panel = function(x, y,...) {
panel.xyplot(x,y,...)
panel.text(MIN.X+(0.1*MAX.X),
MAX.Y-(0.1*MAX.Y),
labels=LABELS[panel.number()],
cex=0.3
)
}
)
A similar plot also add the autokey because it takes in account two
different Y values, but the plot is not drawn rather the function is
implemented but the plot remains empty:
xyplot(Y1+Y2 ~ X | Z,
data = DATAFRAME,
ylab= "Y",
xlab="X",
main="TITLE",
scales = list(
x = list(draw = FALSE),
y = list(draw = FALSE),
relation="same",
alternating=TRUE),
as.table = TRUE,
layout = LAYOUT,
auto.key= list(space = "centre"),
par.settings = list(
strip.background=list(col="white"),
axis.text = list(cex = 0.6),
par.xlab.text = list(cex = 0.75),
par.ylab.text = list(cex = 0.75),
par.main.text = list(cex = 0.8),
superpose.symbol = list(pch = ".", cex = 1)
),
strip = FALSE,
type = "l",
col = c(4,2),
key = list(
space="top", columns=2,
text=list(TEXT_FOR_AUTOKEY, col="black"),
lines=list(col=c(4,2)),
panel = panel.superpose
),
panel = function(x, y,...)
{
panel.xyplot(x,y,...)
panel.text(MIN.X+(0.1*MAX.X),
MAX.Y-(0.1*MAX.Y),
labels=LAB[panel.number()],
cex=0.3,
panel = panel.superpose
)
}
)
I am not attaching actual data because I believe the problem is in the
actual call, maybe I am using twice panel.superimpose, although
several combination I made (for instance moving the key argument into
the panel function) did not solve the problem.
Could you tell me where I am getting it wrong?
Thank you.
best regards
Luigi
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list