forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
44 lines (33 loc) · 1.07 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
34
35
36
37
38
39
40
41
42
43
44
#plot2.R
importData<-function(){
# Setup
#install.packages("dplyr")
#install.packages("data.table")
#install.packages("lubridate")
library(dplyr)
library(data.table)
library(lubridate)
#Read in the file
full_file<-fread("household_power_consumption.txt",header=TRUE)
#Conversion to dates
f<-mutate(full_file,DateTime=dmy_hms(paste(Date,Time)))
#Filter to just 2007-02-01 and 2007-02-02
data<-filter(f,year(DateTime)==2007,month(DateTime)==02,day(DateTime)==01|day(DateTime)==02)
#Convert to numbers and omit NA
data$Sub_metering_1<-as.numeric(data$Sub_metering_1)
data$Sub_metering_2<-as.numeric(data$Sub_metering_2)
data$Sub_metering_3<-as.numeric(data$Sub_metering_3)
data$Global_active_power<-as.numeric(data$Global_active_power)
data
}
#Plot the Graph
plot2<-function(){
data<-importData()
par(mfrow=c(1,1))
with(data,{
plot2<-plot(DateTime,Global_active_power,type="l",ylab="Global Active Power (kilowatts)",xlab="")
})
#Save it to a file
dev.copy(png, file = "plot2.png",width=480,height=480)
dev.off()
}