forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
32 lines (28 loc) · 1.44 KB
/
plot3.R
File metadata and controls
32 lines (28 loc) · 1.44 KB
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
# plot 3
rm(list=ls())
#
#Define file name to save and target url
zipfilename<-"Power_Dataset.zip"
zipfileurl<-"https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
#check if already downloaded and un-ziped before, otherwise download and unzip the data
if (!file.exists(zipfilename)){
download.file(url=zipfileurl, destfile=zipfilename)
unzip(zipfilename)
}
powerfile <- "household_power_consumption.txt"
# read file with data
all_data <- read.table(file=powerfile, header=TRUE, sep = ";", na.strings = "?", colClasses = c("character","character","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
# calculate time from date and time and add newtime column in the data
all_data$newtime <- strptime(paste(all_data$Date, all_data$Time, sep=" "),format= "%d/%m/%Y %H:%M:%S")
# change format for date
all_data$Date=as.Date(all_data$Date, format="%d/%m/%Y")
# create final data
final_data <- subset(all_data, Date >= "2007-02-01" & Date<="2007-02-02")
#png device
png(file='plot3.png',width = 480,height = 480)
plot(final_data$newtime,final_data$Sub_metering_1,type='n',ylab="Energy Sub metering",xlab='')
lines(final_data$newtime,final_data$Sub_metering_1)
lines(final_data$newtime,final_data$Sub_metering_2,col = 'red')
lines(final_data$newtime,final_data$Sub_metering_3,col = 'blue')
legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),lty=c(1,1,1),col=c("black","red","blue"))
dev.off()