forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
37 lines (32 loc) · 1.87 KB
/
plot2.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
#############################################################################
### Function to plot house hold power consumption explororatory data ########
### ########
#############################################################################
plot2 <- function() {
### Creating directory data if it doesn't exist
### Down load the zip file and unzip it under data
if(!file.exists("./data")) {dir.create("./data")}
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(fileUrl, destfile = "./data/HHPowCons.zip", method = "curl")
zipF <- "./data/HHPowCons.zip"
outDir<-"./data"
unzip(zipF,exdir=outDir)
### Reading the data and then converting the column types
hpcdata <- read.table("./data/household_power_consumption.txt", sep = ";", header = TRUE)
names(hpcdata)[1] <- "date"
hpcdata$date <- as.Date(hpcdata$date, "%d/%m/%Y")
hpcdata$date <- as.Date(hpcdata$date, format = "%Y-%m-%d")
hpcdata_dt <- hpcdata[(hpcdata$date == "2007-02-01" | hpcdata$date == "2007-02-02"),]
temp <- strptime((paste(hpcdata_dt$date, hpcdata_dt$Time, sep = " ")), format = "%Y-%m-%d %H:%M:%S")
hpcdata_dt$datetime <- temp
hpcdata_dt$Global_active_power <- as.numeric(hpcdata_dt$Global_active_power)
hpcdata_dt$Global_reactive_power <- as.numeric(hpcdata_dt$Global_reactive_power)
hpcdata_dt$Voltage <- as.numeric(hpcdata_dt$Voltage)
hpcdata_dt$Sub_metering_1 <- as.numeric(hpcdata_dt$Sub_metering_1)
hpcdata_dt$Sub_metering_2 <- as.numeric(hpcdata_dt$Sub_metering_2)
hpcdata_dt$Sub_metering_3 <- as.numeric(hpcdata_dt$Sub_metering_3)
## Plot for Global Active power
png(filename="./figure/plot2.png", height=480, width=480)
plot(hpcdata_dt$datetime, hpcdata_dt$Global_active_power/500, xlab="", ylab = "Global Active Power (kilowatts)", type = "l")
dev.off()
}