forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALL.R
101 lines (76 loc) · 2.97 KB
/
ALL.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
## set date.pattern for filtering
date.pattern <- '[12]/2/2007'
# check whether file has been downloaded, no need to redo download
if (!file.exists('household_power_consumption.zip')) {
download.file('https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip',
'household_power_consumption.zip')
}
# check whether file has been unzipped, no need to redo
if (!file.exists('household_power_consumption.txt')) {
unzip('household_power_consumption.zip')
}
print('txt file is read. Load file content to memory ...')
date.pattern <- '[12]/2/2007'
lines<- readLines(('household_power_consumption.txt'))
print('Read txt file completed, Prepare data frame ...')
## extract header
line.header<-head(lines,1)
line.names<- unlist(strsplit(line.header, split=';'))
## handle data
lines.filter<-lines[grep('^[12]/2/2007',lines)]
lines.list<- strsplit(lines.filter,';')
# convert to dataFrame
dat<-as.data.frame(do.call(rbind, lines.list), stringsAsFactors=F)
# assign name
colnames(dat) <- line.names
#handle datetime
dat$DT <- strptime( paste(dat[,1], dat[,2]), format="%d/%m/%Y %H:%M:%S")
dat[,1]=NULL
dat[,1]=NULL
# re-format measures
for(x in 1:7) { dat[,x] <- as.numeric(dat[,x])}
# data prepartion completed.
print ('data prepartion completed. Generation graphs ...')
#Plot 1
png(filename='plot1.png', width=480, height=480, units='px')
hist(dat$Global_active_power, col = 'red', xlab='Global Active Power (kilowatts)', main = 'Global Active Power')
dev.off()
#Plot 2
png(filename='plot2.png', width=480, height=480, units='px')
plot(dat$DT, dat$Global_active_power, type='l', ylab= 'Global Active Power (kilowatts)', xlab='')
dev.off()
#Plot 3
png(filename='plot3.png', width=480, height=480, units='px')
plot(dat$DT, dat$Sub_metering_1, type='l',
ylab= 'Energy sub metering',
xlab= '');
lines(dat$DT, dat$Sub_metering_1);
lines(dat$DT, dat$Sub_metering_2, col='red');
lines(dat$DT, dat$Sub_metering_3, col = 'blue');
legend('topright',
legend=c('Sub_metering_1', 'Sub_metering_2','Sub_metering_3'),
col=c('black','red','blue'), # legend line color
lty=1)
dev.off()
#4.1
png(filename='plot4.png', width=480, height=480, units='px')
par(mfcol=c(2,2))
par(mar=c(5,4,3,2))
plot(dat$DT, dat$Global_active_power, type='l', ylab= 'Global Active Power (kilowatts)', xlab='')
#4.2
plot(dat$DT, dat$Sub_metering_1, type='l',
ylab= 'Energy sub metering', xlab='');
lines(dat$DT, dat$Sub_metering_1);
lines(dat$DT, dat$Sub_metering_2, col='red');
lines(dat$DT, dat$Sub_metering_3, col = 'blue');
legend('topright',
legend=c('Sub_metering_1', 'Sub_metering_2','Sub_metering_3'),
col=c('black','red','blue'),
lty=1,
bty='n') # legend no border
#4.3
plot(dat$DT, dat$Voltage, type='l', ylab='Voltage', xlab='datetime')
#4.4
plot(dat$DT, dat$Global_reactive_power, type='l', ylab='Gloabal_reactive_power', xlab='datetime', ylim=c(0,0.5))
dev.off()
print('All done !')