-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlattice_example_1.R
76 lines (59 loc) · 2.29 KB
/
lattice_example_1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#---------- lattice Example #1------------#
# http://www.statmethods.net/advgraphs/trellis.html
library(lattice)
attach(mtcars)
## Simple scatterplot
xyplot(mpg ~ hp, data = mtcars)
## More complex:
# Create factors with value labels
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
# Kernel density plot
densityplot(~mpg,
main="Density Plot",
xlab="Miles per Gallon")
# Kernel density plots by factor level
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon")
# Kernel density plots by factor level (alternate layout)
densityplot(~mpg|cyl.f,
main="Density Plot by Numer of Cylinders",
xlab="Miles per Gallon",
layout=c(1,3))
# Boxplots for each combination of two factors
bwplot(cyl.f~mpg | gear.f,
ylab="Cylinders", xlab="Miles per Gallon",
main="Mileage by Cylinders and Gears",
layout=c(1,3))
# Scatterplots for each combination of two factors
xyplot(mpg ~ wt|cyl.f*gear.f,
main="Scatterplots by Cylinders and Gears",
ylab="Miles per Gallon", xlab="Car Weight")
# 3d scatterplot by factor level
cloud(mpg ~ wt*qsec|cyl.f,
main="3D Scatterplot by Cylinders")
# Dotplot for each combination of two factors
dotplot(cyl.f~mpg|gear.f,
main="Dotplot Plot by Number of Gears and Cylinders",
xlab="Miles Per Gallon")
# Scatterplot matrix
splom(mtcars[c(1,3,4,5,6)],
main="MTCARS Data")
# Levelplot:
require(geoR)
data(elevation)
elevation.df = data.frame(x = 50 * elevation$coords[,"x"],
y = 50 * elevation$coords[,"y"], z = 10 * elevation$data)
elevation.loess = loess(z ~ x*y, data = elevation.df, degree = 2, span = 0.25)
elevation.fit = expand.grid(list(x = seq(10, 300, 1), y = seq(10, 300, 1)))
z = predict(elevation.loess, newdata = elevation.fit)
elevation.fit$Height = as.numeric(z)
levelplot(Height ~ x*y, data = elevation.fit,
xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
main = "Surface elevation data",
col.regions = terrain.colors(100)
)
#------------ End of lattice Example #1 ------------#