Earlier, I asked about this question , which was useful when building a function. I want to try to build twenty functions on the same axes to illustrate how a function changes between two ranges. I successfully did this using individually defined functions, but I wanted to do it with a loop.
I tried to do the following:
library(ggplot2)
library(lattice)
inPath = "D:/R_Analysis/"
inFile = "sample.txt"
outPath = "D:/R_Analysis/"
outFile = "processed_sample.txt"
pdfOutPath = "D:/R_Analysis/"
pdfOutFile = "processed_sample.pdf"
y_label = "x-axis"
x_label = "y-axis"
chart_title = "..."
analysis <-
read.table(paste(inPath, inFile, sep=""), header=TRUE, sep=",",
na.strings="NA", dec=".", strip.white=TRUE)
pdf(paste(pdfOutPath, pdfOutFile, sep=""),height=6,width=9)
p <- qplot(
data = data.frame(x = x, y = y), x, y, xlab = x_label, ylab = y_label,
enter code herexlim = x_range, main = chart_title )
eq_dummy = function(x){ 0 }
d = stat_function(fun = eq_dummy)
for(i in 1 : 21){
intercept = analysis[i,2]
slope = analysis[i,3]
eq <- function(x) { slope * log(x) + intercept }
composite <- stat_function(fun=eq)
composite = composite + d
}
print(p + composite)
warnings()
dev.off()
Any suggestions for improving the syntax or programming structure would be appreciated. Thank.
source
share