forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
58 lines (44 loc) · 1.65 KB
/
plot4.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
56
57
58
#plot4.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
}
plot4<-function(){
data<-importData()
par(mfrow=c(2,2))
#Top Left
with(data,{
plot2<-plot(DateTime,Global_active_power,type="l",ylab="Global Active Power (kilowatts)",xlab="")
})
#Top Right
plot(data$DateTime,data$Voltage,type="l",xlab="datetime",ylab="Voltage")
#Bottom Left
plot(data$DateTime,data$Sub_metering_1,type="l",ylab="Energy sub metering",xlab="")
points(data$DateTime,data$Sub_metering_2,col="red",type="l")
points(data$DateTime,data$Sub_metering_3,col="blue",type="l")
legend("topright", lty = 1, cex=.5, col = c("black","red","blue"),
legend = c("Sub_metering_1", "Sub_metering_2","Sub_metering_3"))
#Bottom Right
plot(data$DateTime,data$Global_reactive_power,type="l",xlab="datetime",ylab="Global_reactive_power")
#Save it to a file
dev.copy(png, file = "plot4.png",width=480,height=480)
dev.off()
par(mfrow=c(1,1))
}