Attachment 'lattice_tut.R'
Download 1 library(lattice)
2 library(lme4)
3
4 ## data set
5 data(sleepstudy)
6 attach(sleepstudy)
7
8 ## graphics devices
9 quartz() # on OSX, interactive
10 ## X11() # on linux, interactive
11 ## pdf() # default for OSX, save to a PDF file
12 ## png() # default on Linux, save to a PNG file
13
14
15 ## 1 variable plots
16 histogram(Reaction)
17 densityplot(Reaction)
18
19 ## 2 variable plots
20 barchart(Reaction ~ Days) # wrong
21 barchart(mean(Reaction) ~ Days) # wrong
22
23 ## aggregate data before using a barchart
24 d <- aggregate(Reaction, by=list(Days=Days), FUN=mean)
25
26 barchart(x ~ Days, d)
27 barchart(x ~ Days, d, horizontal=FALSE) # we usually want columns
28
29
30 ## scatter plots
31 xyplot(Reaction ~ Days)
32 xyplot(Reaction ~ Days, type = "l") # line plot
33 xyplot(Reaction ~ Days, type = "r") # best fit linear regression line
34 xyplot(Reaction ~ Days, type = "a") # line plot of the average
35 xyplot(Reaction ~ Days, type = "smooth") # smoothed line fit to the data
36 xyplot(Reaction ~ Days, type = "p") # points
37
38 ## grouping
39 xyplot(Reaction ~ Days, group = Subject)
40 xyplot(Reaction ~ Days, group = Subject, auto.key=TRUE)
41 xyplot(Reaction ~ Days, group = Subject, type = "a", auto.key=TRUE)
42
43 ## panels
44 xyplot(Reaction ~ Days | Subject)
45 xyplot(Reaction ~ Days | Subject, type = "a")
46
47 ## panels and groups
48 ## fake up some groups
49 d <- sleepstudy
50 d$Condition <- rep(c(1,2))
51
52 xyplot(Reaction ~ Days | Condition, group = Subject,
53 data = d,
54 type = "l")
55
56 xyplot(Reaction ~ Days | Subject, group = Condition,
57 data = d,
58 type = "l")
59
60 ## error bars with lattics
61 plotting functions based on demo("intervals", package="lattice")
62 file.show(system.file("demo/intervals.R", package = "lattice"))
63
64 prepanel.ci.v <- function(x, y, ly, uy, subscripts, ...)
65 {
66 y <- as.numeric(y)
67 ly <- as.numeric(ly[subscripts])
68 uy <- as.numeric(uy[subscripts])
69 list(ylim = range(y, uy, ly, finite = TRUE))
70 }
71
72 panel.ci.v <- function(x, y, ly, uy, subscripts, ...)
73 {
74 x <- as.numeric(x)
75 y <- as.numeric(y)
76 ly <- as.numeric(ly[subscripts])
77 uy <- as.numeric(uy[subscripts])
78 panel.barchart(x, y, ...)
79 panel.arrows(x, ly, x, uy, col = 'black',
80 length = 0.25, unit = "native",
81 angle = 90, code = 3)
82 }
83
84 prepanel.ci.h <- function(x, y, lx, ux, subscripts, ...)
85 {
86 x <- as.numeric(x)
87 lx <- as.numeric(lx[subscripts])
88 ux <- as.numeric(ux[subscripts])
89 list(xlim = range(x, ux, lx, finite = TRUE))
90 }
91
92
93 panel.ci.h <- function(x, y, lx, ux, subscripts, pch = 21, ...)
94 {
95 x <- as.numeric(x)
96 y <- as.numeric(y)
97 lx <- as.numeric(lx[subscripts])
98 ux <- as.numeric(ux[subscripts])
99 panel.abline(h = unique(y), col = "gray")
100 panel.arrows(lx, y, ux, y, col = "black",
101 length = 0.25, unit = "native",
102 angle = 90, code = 3)
103 panel.xyplot(x, y, pch = pch, ...)
104 }
105
106 ## once you've defined ux and lx, this will work
107 xyplot( Reaction ~ Days | Subject,
108 lx = lower, ux = upper,
109 prepanel = prepanel.ci.h,
110 panel = function (x, y, subscripts=subscripts, lx=lx, ux=ux, ...)
111 {
112 panel.ci.h(x, y, lx=lx, ux=ux, subscripts=subscripts,...)
113 panel.abline(v = 0,
114 col = "black")
115 })
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.