Let's pay attention to this data:

Let's use Lattice on some of these data!
The data has been uploaded to GoogleDrive, and we will use from there:
filename = "https://docs.google.com/spreadsheet/pub?key=0AhVqDdZgThPldEhFWHpkeGNUaHJkcjA4NUlyc0hZcnc&single=true&gid=0&output=csv"
library(RCurl)
myCsv <- getURL(filename)
DowJones = read.csv(textConnection(myCsv), row.names = 1, header = TRUE)
head(DowJones)
## Weekly Yearly
## 3M Co 2.1 -1.9
## Alcoa Inc 2.6 -36.7
## AmExp -0.4 11.9
## AT&T Inc 4.4 3.7
## BoA 3.1 -46.7
## Boeing 1.3 5.7
In this case, we are considering the first column “Company” as the row names and telling R that the first row has the names of the variables. However, we may need to use the Company values for our examples, so we'd better jus use numbers as row names:
DowJones = read.csv(textConnection(myCsv), header = TRUE)
head(DowJones)
## Company Weekly Yearly
## 1 3M Co 2.1 -1.9
## 2 Alcoa Inc 2.6 -36.7
## 3 AmExp -0.4 11.9
## 4 AT&T Inc 4.4 3.7
## 5 BoA 3.1 -46.7
## 6 Boeing 1.3 5.7
SORTING COMPANY NAMES
As the head commands show, the contest of the file are presented in the original order of the file.
library(lattice)
par(font.sub=1)
dotplot(Company~Yearly,type='p', # plot points
col="red",cex=1.5, # set the color and size
data=DowJones,
xlab="Yearly Percent Change",
sub="From the Washington Post: Jan 28, 2012",
main="30 Dow Jones Industrials")
As you can see, the first value, “3M Co” appears at the bottom, and “Walt Disney” appears at the top, following the usual convention for this case. However, we now use some ordering to modify the plot:
ord <- order(DowJones$Yearly)
levelSort <- levels(DowJones$Company)[ord]
DowJones$Company <- factor(DowJones$Company,levels=levelSort, ordered=TRUE)
dotplot(Company~Yearly,type='p', # plot points
col="blue",cex=1.1, # set the color and size
data=DowJones,
xlab="Yearly Percent Change",
sub="From the Washington Post: Jan 28, 2012",
main="30 Dow Jones Industrials")
Now, let's plot both weekly and yearly values:
nam = as.character(DowJones$Company)
levelSort <- levels(DowJones$Company)
DowJonesStack <- data.frame(
Company = factor(c(nam,nam),
level=levelSort, ordered=TRUE),
PercentChange = c(DowJones$Weekly,DowJones$Yearly),
Period = c(rep("Weekly",30),rep("Yearly",30))
)
my.key <-
list(space="right",
text=list(levels(DowJonesStack$Period)),
points=list(pch=c(17,16), #17 = filled triangles / 16 = filled circles
cex=c(1.3,1.1),
col=c(rgb(0,153,0,maxColorValue = 255),rgb(255,0,255,maxColorValue = 255))))
dotplot(Company ~ PercentChange,
groups=Period,
data=DowJonesStack,
pch=c(17,16),
cex=c(1.3,1.1),
col=c(rgb(0,153,0,maxColorValue = 255),rgb(255,0,255,maxColorValue = 255)),
key=my.key,
main="Thirty Dow Jones Companies, Jan. 28, 2012")
LATTICE FOR MULTIPLE PANEL PLOTS
Let's see some functionality that Lattice has for this situation. In this case we will use the data singers which is included in Lattice:
lab1 = "Density of Heights (inches) of the singers"
lab2 = "in the New York Choral Society, 1979"
lab3 = "grouped by vocal range"
densityplot(~height | voice.part, data = singer, col = "red", layout = c(2,
4), main = paste(lab1, lab2, lab3, sep = "\n"))
MODIFYING PANEL FUNCTIONS
In ths situation, we will go deeper amd modify the panel functions. First, boxplots:
bwplot(~height | voice.part, data = singer, layout = c(2, 4), panel = function(...) {
panel.fill(rgb(229, 255, 204, 50, maxColorValue = 255))
panel.grid(h = -1, v = -1, col = rgb(224, 224, 224, maxColorValue = 255),
lwd = 1)
panel.bwplot(col = "red", cex = 1, ...)
})
… and second, Q-Q plots:
qqmath(~height | voice.part, data = singer, layout = c(2, 4), pch = 16, cex = 1,
panel = function(...) {
panel.fill(rgb(229, 255, 204, 90, maxColorValue = 255))
panel.grid(h = -1, v = -1, col = rgb(224, 224, 224, maxColorValue = 255),
lwd = 1)
panel.qqmathline(..., col = "black", lwd = 1)
panel.qqmath(..., col = "red")
})