forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
44 lines (40 loc) · 1.5 KB
/
plot3.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
##find skip
skip_find<-function(inputFile,pattern,header=T){
con <- file(inputFile, open = "r")
if(header==T)
readLines(con, n = 1, warn = FALSE)#header
sk<-0
while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {
sk<-sk+1
if(length(grep(pattern,oneLine))==1)
break
}
close(con)
sk
}
##read data
df<-read.table(file="household_power_consumption.txt",header = F,sep=";",
na.strings = "?",stringsAsFactors = F,
skip=skip_find("household_power_consumption.txt","^1/2/2007"),
nrows = 2*24*60)
colnames(df)<-colnames(read.table(file="household_power_consumption.txt",header = T,sep=";",na.strings = "?",stringsAsFactors = F,nrows = 1))
df$Date<-as.Date(df$Date,"%d/%m/%Y")
df$Time<-strptime(paste(df$Date,df$Time),"%Y-%m-%d %H:%M:%S")
with(df,{
plot(Time,Sub_metering_1,type="n",xlab = "",ylab="Energy sub metering")
lines(Time,Sub_metering_1,col="grey")
lines(Time,Sub_metering_2,col="red")
lines(Time,Sub_metering_3,col="blue")
legend("topright",lty=1,col=c("grey","red","blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
bty="n")
})
png(file="plot3.png")
with(df,{
plot(Time,Sub_metering_1,type="n",xlab = "",ylab="Energy sub metering")
lines(Time,Sub_metering_1,col="grey")
lines(Time,Sub_metering_2,col="red")
lines(Time,Sub_metering_3,col="blue")
legend("topright",lty=1,col=c("grey","red","blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
bty="n")
})
dev.off()