forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plot2.R
17 lines (17 loc) · 1.13 KB
/
Plot2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## Script to load and make plots from household_power_consumption data
## Get the URL
fileURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zipdatafile <- "./datafile.zip" ## create zipdata file
download.file(fileURL, destfile = zipdatafile, mode = "wb") ## download the file
unzip(zipdatafile) ## unzip the file
Sys.setlocale("LC_TIME", "English") # set local time to english to show the graphs correct
## read the only the data from dates: 1/2/2007, 2/2/2007
hpcdata <- subset(read.csv("household_power_consumption.txt", sep = ";",
na.strings = "?", stringsAsFactors = FALSE), Date %in% c("1/2/2007", "2/2/2007"))
hpcdata$Date_Time <- paste(hpcdata$Date, hpcdata$Time) ## concatenate data and time
## transform date and time to date and time class
hpcdata$Date_Time <- strptime(hpcdata$Date_Time, format="%d/%m/%Y %H:%M:%S")
png("plot2.png") ## initiate png graphic device, the standard is 480x480px
## make the plot from data
plot(hpcdata$Date_Time, hpcdata$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power (kilowatts)")
dev.off() ## Close the device (png)