-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistwithfunctionsandloop.R
101 lines (83 loc) · 2.6 KB
/
histwithfunctionsandloop.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# R plots for histogram example
# Now with functions (subroutines) and a for-loop
# The data are included in R, but here's another way to read them
# in as tab-separated text:
sunspotslocal <- read.table("sunspots.dat", sep="\t", header=TRUE)
sunspotdensity <- sunspotslocal
# set up the size of the bins for each histogram:
breaks<-c(4,10,20,30)
# rendertype <- ("windows")
rendertype <- ("quartz")
# pieces of each file name:
filenamestem<-("sunspothistogram")
filenamepost<-("bins")
# A function to draw a distribution normal curve over the histogram:
addNorm <- function(data,color,linewidth) {
# Add a Normal Curve (Thanks to Peter Dalgaard)
xfit<-seq(min(data),max(data),length=40)
yfit<-dnorm(xfit,mean=mean(data),sd=sd(data))
yfit <- yfit*diff(h$mids[1:2])*length(data)
lines(xfit, yfit, col=color, lwd=linewidth)
return;
}
addGamma <- function(data, color, linewidth){
a <- 1.5
s <- 32
xfit<-seq(min(data),max(data),length=200)
yfit<-dgamma(xfit,shape=a,scale=s, log=FALSE)
yfit <- yfit*diff(h$mids[1:2])*length(data)
lines(xfit, yfit, col=color, lwd=linewidth)
return;
}
# Finally, the histogram loop: draw identical histogram graphics files based on the "breaks" array:
for(i in 1:length(breaks)) {
filenamecur<-paste(filenamestem,breaks[[i]],filenamepost,sep="_")
png(
filename=(paste(filenamecur,"png", sep=".")),
res=300,
bg="white",
type=rendertype,
pointsize=12,
width=6,
height=6,
units="in")
h<-hist(sunspotdensity$Sunspot.count,
breaks=breaks[[i]],
col="gray",
xlab="Sunspot Count",
main="Sunspot count by year, 1700-2004")
#addNorm(data=sunspotdensity$Sunspot.count,
# color="blue",
# linewidth=2)
addGamma(
data=sunspotdensity$Sunspot.count,
color="blue",
linewidth=2
)
dev.off()
}
# Make a loop-friendly, but less descriptive, name for the histogram:
# slightly alter the filename stem:
filenamestem<-paste(filenamestem,'0',sep="")
for(i in 1:length(breaks)) {
# make a new filename prefix:
filenamecur<-paste(filenamestem,i,sep="")
png(filename=(paste(filenamecur,"png", sep=".")),
res=300, bg="white",
type=rendertype,
pointsize=12,
width=6, height=6, units="in"
)
h<-hist(sunspotdensity$Sunspot.count,
breaks=breaks[[i]],
col="gray",
xlab="Sunspot Count",
main="Sunspot count by year, 1700-2004")
# addNorm(data=sunspotdensity$Sunspot.count,color="blue",linewidth=2)
addGamma(
data=sunspotdensity$Sunspot.count,
color="blue",
linewidth=2
)
dev.off()
}