forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
29 lines (25 loc) · 1.53 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
#This function requires the lubridate and plyr packages
plot2 <- function(){
#load required libraries
library(lubridate)
library(plyr)
#open the file (we found out from previous analysis thSysat the rows with the required data are the ones between 66637 and 69516 , both included)
#here's the filter code, for reference, even if we don't need it anymore
#electric<- electric[electric$Date >= dmy("01-02-2007") & electric$Date <=dmy("02-02-2007"),]
#read first line only to use as headers, then skip the uninteresting data to save memory
headers <- read.table("household_power_consumption.txt", na.strings="?", sep=";",header=FALSE, nrow=1)
electric <- read.table("household_power_consumption.txt", na.strings="?", sep=";",
header=FALSE, skip = 66637, nrow=2880)
colnames(electric)<-unlist(headers)
#mutate the Date and Time columns into proper date and time objects
electric <- mutate(electric, Date = dmy(Date))
electric <- mutate(electric, Time = hms(Time))
#The code up to this part could be saved in a different file for reutilization
#on different plots, but since it is an assigment, we'll copy it for clarity.
electric <- mutate(electric, DateTime = Date+Time)
#if you don't have English as your native language, remember to set the locale!
#Sys.setlocale("LC_TIME", "English")
png("plot2.png",480,480)
with(electric, plot(DateTime,Global_active_power, type="l", xlab = "", ylab="Global Active Power (kilowatts)"))
dev.off()
}