Suppose I am in this case:
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",")
nba <- nba[order(nba$PTS),]
row.names(nba) <- nba$Name
nba_matrix <- data.matrix(nba)
library(ggplot2)
library(reshape)
nba.m <- melt(nba)
library(plyr)
library(scales)
nba.m <- ddply(nba.m, .(variable), transform, rescale=rescale(value))
(p <- ggplot(nba.m, aes(variable, Name)) + geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue"))
Now I want to create a series of images contained in the same pdf document (not n pdf documents):
listaNbA <- list(nba.m,nba.m)
pdf("bennagoal.pdf")
(p <- ggplot(listaNbA[[1]], aes(variable, Name)) + geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "white", high = "purple")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_text(aes(label=value))
)
(p <- ggplot(listaNbA[[2]], aes(variable, Name)) + geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "white", high = "purple")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_text(aes(label=value))
)
dev.off()
It works producing a pdf file with two pages. Now I want to do the same, but using a for loop:
listaNbA <- list(nba.m,nba.m)
pdf("jamesotto.pdf")
for(i in 1:2){
(p <- ggplot(listaNbA[[i]], aes(variable, Name)) + geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "white", high = "purple")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_text(aes(label=value))
)
}
dev.off()
The second code produces an empty pdf file. Why?