I'm not having any success combining lapply, substitute, print, plot, and list in a function statement. Can someone direct me to a way to revise this code? I'd like to:
- automate plotting some but not all fields in a data frame vs. the first field in the df
- specify the fields to plot
- write the result to a file
The fields are numeric. Thanks.
brg<-as.data.frame(sqlFetch(channel,"Bearing"))
is.data.frame(brg)
varlist<-names(brg)[c(6,8,10,12)]
varlist
pdf(file="brg%d.pdf")
figures<-lapply(varlist,function(x) {
print(plot(substitute(brg[c(1,i)], (list(i = as.name(x))))))
})
update: Thanks for the comments. Here are two working versions of this code with axis lables (df read from SQL Server as written):
#vers. 1
brg<-as.data.frame(sqlFetch(channel,"Bearing"))
is.data.frame(brg)
dim(brg)
head(brg)
names(brg)<-tolower(names(brg))
names(brg)
varlist<-names(brg)[c(6,8,10,12)]
varlist
pdf(file="brgd.pdf")
for (x in varlist) plot(brg[,1],brg[,x], xlab=names(brg[1]),ylab=as.name (x))
dev.off()
#vers. 2
brg<-as.data.frame(sqlFetch(channel,"Bearing"))
is.data.frame(brg)
dim(brg)
head(brg)
names(brg)<-tolower(names(brg))
names(brg)
varlist<-names(brg)[c(6,8,10,12)]
varlist
pdf(file="brg1.pdf")
figures<-lapply(varlist,function(x) {
(plot(brg[,1],brg[,x], ylab=as.name(x)))
})
dev.off()