forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
55 lines (43 loc) · 2.23 KB
/
plot3.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
49
50
51
52
53
54
55
# This is the R script created for Exploratory Data Analysis Course Project 1
# Its goal is to reproduce plot3.png using Global Active Power data over a
# 2-day period in February, 2007 in the Electric Power consumption data set.
# The script assumes that the input data set is in the working directory.
# In case the input data set is missing the script downloads and extracts it
# if necessary.
# sqldf package is used to subset the data set as it is read
library(sqldf)
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zipFile <- "household_power_consumption.zip"
dataFile <- "household_power_consumption.txt"
# Download and unzip the data set as necessary
if (!file.exists(dataFile)) {
if (!file.exists(zipFile)) {
download.file(url=fileUrl, destfile=zipFile, method="curl")
dateDownloaded <- date()
}
unzip(zipFile)
}
# Read and subset the data set with read.cvs.sql in packege sqldf
powercons <- read.csv.sql(dataFile, sep=';',
sql='SELECT * FROM file WHERE Date="1/2/2007" OR Date="2/2/2007"')
# Convert Time and Date to usable format
powercons$Time <- strptime(paste(powercons$Date, powercons$Time), format="%d/%m/%Y %H:%M:%S")
powercons$Date <- as.Date(powercons$Time)
# It might be necessary to set the Time Locale on some systems to use English
# This is OS platform specific - tested on Ubuntu Linux 14.04 LTS and R 3.1.1
Sys.setlocale("LC_TIME", "C")
# Open png graphics device to construct plot3.png as 480x480 PNG image
png(filename="plot3.png", width=480, height=480)
# Plot Sub_metering variables as a function of Time to reconstruct plot3
plot(powercons$Time, powercons$Sub_metering_1, type="l", xlab="",
ylab="Energy sub metering")
lines(powercons$Time, powercons$Sub_metering_2, col="red")
lines(powercons$Time, powercons$Sub_metering_3, col="blue")
# Add legend on top right
legend("topright", lwd=1, col=c("black", "red", "blue"),
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
# Close the png graphics device
dev.off()
# Please ignore the "closing unused connection" warnings that might
# be displayed on some platforms. No solution found so far:
# https://class.coursera.org/exdata-005/forum/thread?thread_id=42