[R] How to overlay lines and rectangles in lattice plot key
Duncan Mackay
dulcalma at bigpond.com
Thu Dec 29 08:26:11 CET 2016
I forgot to change the xyplot for the colours; if you want the same colours
in the key you need to use the par.settings argument or set the settings for
the device.
the xyplot becomes
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
iris,
par.settings = list(superpose.symbol = list(col = 1:4,
cex = 1,
pch = 1:4),
superpose.line = list(col = 1:4)
),
type = c("p", "r"),
jitter.x = TRUE,
jitter.y = TRUE,
factor = 5,
legend = list(right = list(fun = XY04.fmG))
)
Duncan
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan
Mackay
Sent: Thursday, 29 December 2016 18:21
To: R
Subject: Re: [R] How to overlay lines and rectangles in lattice plot key
Hi
It makes it easier
library(grid)
library(lattice)
# legend
XY04.glay <-
grid.layout(nrow = 4,
ncol = 2,
heights = unit(rep(1, 2), rep("cm", 2)),
widths = unit(c(0.4, 0.8),
c("in","in")),
just = "centre")
XY04.fmG <- frameGrob(layout = XY04.glay)
k <-0
for (j in seq_len(4)){
XY04.fmG <-
placeGrob(XY04.fmG, textGrob(lab = letters[j],
just = 0,
gp = gpar(cex = 0.8)), row = j, col = 1)
XY04.fmG <-
placeGrob(XY04.fmG, rectGrob(width = 0.6,
gp = gpar(col=j,
alpha = 0.25,
fill = j)), row = j, col = 2)
XY04.fmG <-
placeGrob(XY04.fmG, linesGrob(c(0.2,0.8), c(0.5, 0.5),
gp = gpar(col = j)), row = j, col = 2)
XY04.fmG <-
placeGrob(XY04.fmG, pointsGrob(x = unit(1, "cm"), y = unit(0.5, "npc"),
pch = j,
#width = unit(2, "cm"),
gp = gpar(cex = 0.7, col = j)), row = j, col =
2)
}
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
iris,
type = c("p", "r"),
jitter.x = TRUE,
jitter.y = TRUE,
factor = 5,
legend = list(right = list(fun = XY04.fmG))
)
This gives 1 point instead of 2 or 3.
The OP needs to change the format and fix the heights of the rectangles etc.
I also fixed the colour vectors had j instead of k
Duncan
-----Original Message-----
From: Duncan Mackay [mailto:dulcalma at bigpond.com]
Sent: Thursday, 29 December 2016 16:17
To: R
Subject: RE: [R] How to overlay lines and rectangles in lattice plot key
Hi
Is this something like what you are looking for?
I have put it on the right and heights etc are just a quick guess. (all i
have time for)
It may be a little too complicated for what you want but I thought of this
first based on your description rather than the plot.
library(grid)
library(lattice)
# legend
XY04.glay <-
grid.layout(nrow = 8,
ncol = 2,
heights = unit(rep(1, 2), rep("cm", 2)),
widths = unit(c(0.4, 0.8),
c("in","in")),
just = "centre")
XY04.fmG <- frameGrob(layout = XY04.glay)
k <-0
for (j in seq(1,8,2)){
k = k+1
XY04.fmG <-
placeGrob(XY04.fmG, textGrob(lab = letters[1:4][k],
just = 0,
gp = gpar(cex = 0.8)), row = j, col = 1)
XY04.fmG <-
placeGrob(XY04.fmG, linesGrob(c(0.2,0.8), c(0.5, 0.5),
gp = gpar(col = c(1:4)[k])), row = j, col = 2)
XY04.fmG <-
placeGrob(XY04.fmG, pointsGrob(c(0.2,0.8), c(0.5, 0.5),
pch = j,
gp = gpar(cex = 0.7, col = c(1:4)[k])), row =
j, col = 2)
}
k = 0
for (j in seq(2,8,2)){
k = k+1
XY04.fmG <-
placeGrob(XY04.fmG, rectGrob(width = 0.6,
gp = gpar(col=k,
alpha = 0.25,
fill = k)), row = j, col = 2)
}
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
iris,
type = c("p", "r"),
jitter.x = TRUE,
jitter.y = TRUE,
factor = 5,
legend = list(right = list(fun = XY04.fmG))
)
Have a look at https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html
and the following emails on the thread.
Regards
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 sbihorel
Sent: Thursday, 29 December 2016 13:51
To: r-help at r-project.org
Subject: [R] How to overlay lines and rectangles in lattice plot key
Hi,
I would like to create a custom key for a lattice xyplot in which line
elements are displayed on top of rectangle elements. In the example code
below, the lines and rectangles are shown side by side (the legend
itself is meaningless, but that is not the point). Is there a way to
overlay these key elements (but not the text)?
Thanks
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
iris,
type = c("p", "r"),
jitter.x = TRUE,
jitter.y = TRUE,
factor = 5,
key = list(
column=4,
text=list(lab=letters[1:4]),
lines=list(col=1:4, pch=1:4, type='b'),
rectangles=list(col=1:4, alpha=0.25, border=FALSE)
)
)
______________________________________________
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.
______________________________________________
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