forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
48 lines (40 loc) · 1.69 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
38
39
40
41
42
43
44
45
46
47
48
# Get Data
## if data directory doesn´t exist, create
if (!file.exists("data")) {
dir.create("data")
}
## if data doesn´t exist, download and unzip
if (!file.exists("data/EPC.zip")) {
fileURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zipfile <- "data/EPC.zip"
download.file(fileURL, destfile = zipfile)
unzip(zipfile, exdir = "data")
}
## read data
df <- read.table("data/household_power_consumption.txt", header = TRUE,
sep = ";", na.strings = c("?"), colClasses = c("character",
"character",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric"))
## subset data
subdf <- df[grep("^[1,2]/2/2007", df$Date),]
## create new variable combining date and time
subdf$DateTime <- as.POSIXct(paste(subdf$Date, subdf$Time),
format="%d/%m/%Y %H:%M:%S")
## open png device
png("plot2.png")
## create plot
with(subdf, plot(DateTime,
Global_active_power,
type = "l",
xlab = "",
ylab = "Global Active Power (kilowatts)"
)
)
## close device connection
dev.off()