forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
33 lines (26 loc) · 1.22 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
# Exploratory Data Analysis course (Coursera/Johns Hopkins), Project 1.
# Code to render Plot 2 in the assignment.
# Assumption:
# The “Individual household electric power consumption Data Set” was downloaded from
# the course website and unzipped to the working directory.
# ------------------------------------------
## Preparation of the data (for all four plots) ##
# Read the raw data into R.
temp <- read.table("household_power_consumption.txt", header= TRUE, sep = ";",
na.strings="?", stringsAsFactors = FALSE)
# Subset only the two days specified in the assignment.
workset <- subset(temp, (Date == "1/2/2007" | Date == "2/2/2007"))
# Paste dates and times, and convert to POSIXlt.
workset$DateTime <- paste(workset$Date, workset$Time)
workset$DateTime <- strptime(workset$DateTime, "%d/%m/%Y %H:%M:%S")
# Set locale to get weekdays in English (for non-English systems)
Sys.setlocale("LC_TIME", "en_US")
# ------------------------------------------
## Create Plot 2 ##
# Open PNG device, create file.
png(file = "plot2.png")
# Render plot 2
with(workset, plot(DateTime, Global_active_power, type = "l",
ylab = "Global Active Power (kilowatts)", xlab = ""))
# Close PNG device.
dev.off()