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
43 lines (35 loc) · 1.15 KB
/
plot2.R
File metadata and controls
43 lines (35 loc) · 1.15 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
# Clear Workspace
rm(list=ls())
# Reset Chart Styles
dev.off()
# Import Libraries
library(sqldf)
# Declare Constants
data.file <- "household_power_consumption.txt"
# Download + Unzip File
if (!file.exists(data.file)){
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", destfile = "data.zip", method="curl")
unzip("data.zip")
file.remove("data.zip")
}
# Read Data From Two Dates
data <- read.csv.sql(
data.file,
sql = "SELECT * FROM file WHERE Date='1/2/2007' OR Date='2/2/2007' ORDER BY DATE, TIME",
colClasses= c(rep("character",2), rep("double", 7)),
sep=';',
header=T)
# Convert NAs + Create DateTime Column + Cast Dates + Times
data[data == "?"] <- NA
data$DateTime <- as.POSIXct(paste(data$Date, data$Time, sep=' '), format="%d/%m/%Y %H:%M:%S")
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
# Create Chart
with( data, plot(
Global_active_power ~ DateTime,
ylab = "Global Active Power (kilowatts)",
xlab = "",
type = "l")
)
# Write Chart To PNG
dev.copy(png, file="plot2.png", width=480, height=480)
dev.off()