forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.R
More file actions
39 lines (30 loc) · 1.57 KB
/
plot2.R
File metadata and controls
39 lines (30 loc) · 1.57 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
## Exploratory Data Analysis: Course Project 1
## plot2: generate a 480x480 PNG pilot line chart of Global Active Power
plot2 <- function( ) {
data <- parsefile();
png( filename = "plot2.png", width = 480, height = 480, units = "px",
bg = "transparent" )
with(data, plot(Time, Global_active_power, type="l", xlab="",
ylab="Global Active Power (kilwatts)"))
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
}