forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
31 lines (28 loc) · 1.27 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
# Read in Data File
df = "household_power_consumption.txt"
hpc<-read.delim(df, header=T, sep=";", na.strings="?")
# Convert date/times into datetime format and select subset of data to work with
hpc$datetime = strptime(paste(hpc$Date,hpc$Time), format="%d/%m/%Y %H:%M:%S")
sd = strptime("01/02/2007 00:00:00", format="%d/%m/%Y %H:%M:%S")
ed = strptime("03/02/2007 00:00:00", format="%d/%m/%Y %H:%M:%S")
twoDays<-subset(hpc, datetime>=sd & datetime<ed, select=3:10)
# View updated stats
summary(twoDays)
str(twoDays)
# plot #4 - 4 subplots
png(filename="plot4.png", width=480, height=480)
par(mfrow=c(2,2))
plot(twoDays$datetime, twoDays$Global_active_power,type="l",xlab="",
ylab="Global Active Power")
plot(twoDays$datetime, twoDays$Voltage,type="l",xlab="",ylab="Voltage")
plot(twoDays$datetime, twoDays$Sub_metering_1,type="l",xlab="",
ylab="Energy sub metering")
par(new="T")
lines(twoDays$datetime, twoDays$Sub_metering_2, col="red")
lines(twoDays$datetime, twoDays$Sub_metering_3, col="blue")
legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
lty=rep(1,3), col=c("black","red","blue"))
yname="global_reactive_power"
plot(twoDays$datetime, twoDays$Global_reactive_power,type="l",
xlab=colnames(twoDays)[8],ylab=colnames(twoDays)[2])
dev.off()