Showing posts with label panel function. Show all posts
Showing posts with label panel function. Show all posts
Sunday, September 20, 2015
Apprentice Piece with Lattice Graphs
Lattice graphs can be quite tedious to learn. I don't use them too often and when I need them I usually have to dig deep into the archives for details on the parameter details.
The here presented example may serve as a welcome template for the usage of panel functions, panel ordering, for drawing of lattice keys, etc.
You can download the example data HERE.
(Also, check this resource with examples by the lattice-author).
Read more »
The here presented example may serve as a welcome template for the usage of panel functions, panel ordering, for drawing of lattice keys, etc.
You can download the example data HERE.
(Also, check this resource with examples by the lattice-author).
library(lattice)
df <- read.csv("PATH/TO/Downloads/lattice_data.csv",
header = T, sep = ";")
mypch <- rep(21, 3)
mycol <- c(rgb(0.2, 0.8, 0.9), rgb(0.8, 0.2, 0.9), rgb(0.9, 0.2, 0.2))
stripplot(mean ~ group1 | item_parc, groups = group2, data = df,
type = c("a", "p"), strip = strip.custom(strip.names = c(F, T)),
ylab = "Score-Mittelwert", layout = c(2, 4),
scales = list(y = list(cex = 0.9),
x = list(cex = 1.1, labels = c("männl.", "weibl."),
tck = c(1, 0))),
par.strip.text = list(cex = 0.9),
panel = function(...) {
panel.stripplot(..., col = mycol, pch = mypch, fill = mycol)
},
index.cond = list(c(7,8,5,6,3,4,1,2)),
key = list(space = "bottom", text = list(c("Alter < 35 J.",
"Alter 35-50 J.",
"Alter > 50 J.")),
points = list(col = mycol, cex = 0.8, pch = mypch, fill = mycol),
rep = F))
Lattice Plots - Usage of Panel Functions - Different Axes For Panel-Rows - Alternating Axis Titles
I present code for a stacked graph with common axes only for panels of the same row and with axis titles at different sides. This admittedly took me days (because i had not much of a clue how to use lattice), but eventually I did it and maybe someone can use this for his/her own purpose:
library(lattice)
y1 <- rnorm(120,100,10)
y2 <- rnorm(120,10,1)
facs <- expand.grid(Sites=rep(c("Site I","Site II",
"Site III"),20),Treatment = c("A","B"))
trellis.par.set(clip = list(panel = "off",strip = "off"),
layout.widths = list(right.padding = 10,
left.padding = 8))
PanFun<-function(...){
panel.dotplot(...)
if(is.element(panel.number(),1:2))
{at<-pretty(c(min(y1),max(y1)))
panel.axis("right",at = at,outside = T,
labels = F,half = F)}
if(panel.number() == 3)
{at<-pretty(c(min(y1),max(y1)))
panel.axis("right",at = at,outside = T,
labels = T,half = F)}
if(panel.number() == 4)
{at<-pretty(c(min(y2),max(y2)))
panel.axis("left",at = at,outside = T,
labels = T,half = F)}
if(is.element(panel.number(),5:6))
{at<-pretty(c(min(y2),max(y2)))
panel.axis("left",at = at,outside = T,
labels = F,half = F)}}
dotplot(y1+y2 ~ Treatment|Sites,
outer = TRUE,data = facs,
scales = list(
x = list(rot = 0, tck=c(1,0)),
y = list(draw=F,relation="free",
limits = list(range(y1),range(y1),range(y1),
range(y2),range(y2),range(y2)))),
ylab = list("response one (y1)", y = 0.75, x = -4),
ylab.right = list("response two (y2)", y = 0.25,
rot = 270, x = 4),
xlab = c("Site 1", "Site 2","Site 3"),
strip = FALSE, panel=PanFun)
R package citation:
Sarkar, Deepayan (2008) Lattice: Multivariate Data Visualization with
R. Springer, New York. ISBN 978-0-387-75968-5
See also the thread:
http://www.mail-archive.com/r-help@r-project.org/msg107565.html
..and examples from:
http://lmdvr.r-forge.r-project.org/figures/figures.html
Subscribe to:
Posts (Atom)