forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
32 lines (24 loc) · 1.11 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
#generate the hist graph for plot4.png
library(data.table)
#read the whole data
data <- fread("household_power_consumption.txt")
data$Date <- as.Date(data$Date, format="%d/%m/%Y")
#subset the data
d <- data[data$Date=="2007-02-01" | data$Date=="2007-02-02"]
d$Global_active_power <- as.numeric(d$Global_active_power)
#add date time in data subset
d$datetime <- as.POSIXct(paste(as.Date(d$Date), d$Time))
png(filename = "plot4.png", width = 480, height = 480)
par(mfrow=c(2,2))
#plot 1, top left
plot(d$datetime, d$Global_active_power, type="l", xlab="", ylab="Global Active Power")
#plot 2, top right
plot(d$datetime, d$Voltage, type="l", xlab="datetime", ylab="Voltage")
#plot 3, bottom left
plot(d$datetime, d$Sub_metering_1, xlab = "", ylab = "Energy sub metering", type="l")
lines(d$datetime, d$Sub_metering_2, col="red")
lines(d$datetime, d$Sub_metering_3, col="blue")
legend("topright", col=c("black", "red", "blue"), c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, bty="n")
#plot 4, bottom right
plot(d$datetime, d$Global_reactive_power, type="l", xlab="datetime", ylab="Global_reactive_power")
dev.off()