forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
31 lines (24 loc) · 1.11 KB
/
plot4.R
File metadata and controls
31 lines (24 loc) · 1.11 KB
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
# Plot4
#Import dataset in R
df<-read.table(file="./household_power_consumption.txt",
sep=";",header=TRUE,na.strings="?",stringsAsFactor=FALSE)
#Subset dataset for only dates 1/2/2007 and 2/2/2007
df.sub<-subset(df,Date %in% c("1/2/2007","2/2/2007"))
#Make new variable from Date and Time as POSIXlt class
df.sub$DateTime<-as.POSIXlt(strptime(paste(df.sub$Date,df.sub$Time),format="%d/%m/%Y %H:%M:%S"))
#Plot4
png("./plot4.png",width=480,height=480)
par(mfrow=c(2,2))
plot(df.sub$DateTime,df.sub$Global_active_power,type="l",
xlab="",ylab="Global Active Power")
plot(df.sub$DateTime,df.sub$Voltage,type="l",
xlab="datetime",ylab="Voltage")
plot(df.sub$DateTime,df.sub$Sub_metering_1,type="l",col="black",
xlab="",ylab="Energy sub metering")
lines(df.sub$DateTime,df.sub$Sub_metering_2,col="red")
lines(df.sub$DateTime,df.sub$Sub_metering_3,col="blue")
legend("topright",lty=1,col=c("black","red","blue"),
c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),bty="n")
plot(df.sub$DateTime,df.sub$Global_reactive_power,type="l",
xlab="datetime",ylab="Global_reactive_power")
dev.off()