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
44 lines (35 loc) · 1.85 KB
/
plot3.R
File metadata and controls
44 lines (35 loc) · 1.85 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
33
34
35
36
37
38
39
40
41
42
43
44
## Exploratory Data Analysis: Course Project 1
## plot3: generate a 480x480 PNG pilot line chart of Sub_metering 1-3
plot3 <- function( ) {
data <- parsefile();
png( filename = "plot3.png", width = 480, height = 480, units = "px",
bg = "transparent")
with(data, plot(Time, Sub_metering_1, type="n", xlab="",
ylab="Energy sub metering"))
with(data, lines(Time, Sub_metering_1, col="black"))
with(data, lines(Time, Sub_metering_2, col="red"))
with(data, lines(Time, Sub_metering_3, col="blue"))
legend("topright", lty = 1, col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3") )
dev.off()
}
## parsefile: download and unzip source file if necessary,
## read the relevant lines into a table, and coerce date & time columns
parsefile <- function( ) {
## download file to working directory and unzip, if necessary
if ( !file.exists("household_power_consumption.txt")) {
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip",
method="curl", destfile="exadata-data-household_power_consumption.zip")
system("unzip exadata-data-household_power_consumption.zip")
}
## read file into p, focusing only on observations from 2007-02-01 and 2007-02-02
p <- read.table("household_power_consumption.txt",header=FALSE,sep=";",skip=66637,nrows=2880)
colnames(p) <- c("Date","Time","Global_active_power","Global_reactive_power",
"Voltage","Global_intensity","Sub_metering_1",
"Sub_metering_2","Sub_metering_3")
## convert Date from char to date
p$Date <- as.Date(strptime(p$Date,"%d/%m/%Y"))
p$Time <- as.POSIXlt(strptime(paste(p$Date,p$Time,sep=" "),"%Y-%m-%d %H:%M:%S"))
## return the object
p
}