forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
20 lines (20 loc) · 1.44 KB
/
plot4.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
dataset <- read.table("household_power_consumption.txt", header=TRUE, sep=";")
dataset$Date <- as.Date(dataset$Date, "%d/%m/%Y")
dataset <- dataset[dataset$Date == as.Date("2007-02-01", format="%Y-%m-%d") | dataset$Date == as.Date("2007-02-02", format="%Y-%m-%d"),]
dataset$datetime <- strptime(paste(dataset$Date, dataset$Time, sep=" "), "%Y-%m-%d %H:%M:%S")
dataset$Global_active_power <- as.numeric(as.character(dataset$Global_active_power))
dataset$Sub_metering_1 <- as.numeric(as.character(dataset$Sub_metering_1))
dataset$Sub_metering_2 <- as.numeric(as.character(dataset$Sub_metering_2))
dataset$Sub_metering_3 <- as.numeric(as.character(dataset$Sub_metering_3))
dataset$Voltage <- as.numeric(as.character(dataset$Voltage))
dataset$Global_reactive_power <- as.numeric(as.character(dataset$Global_reactive_power))
png("plot4.png")
par(mfrow=c(2,2), mar=c(4,4,2,2))
with(dataset, plot(datetime, Global_active_power, type="l", xlab="", ylab="Global Active Power"))
with(dataset, plot(datetime, Voltage, type="l", xlab="datetime"))
with(dataset, plot(datetime, Sub_metering_1, type="l", xlab="", ylab="Energy sub metering"))
with(dataset, lines(datetime, Sub_metering_2, col="red", type="l"))
with(dataset, lines(datetime, Sub_metering_3, col="blue", type="l"))
legend("topright", legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lty=1, col=c("black", "red", "blue"))
with(dataset, plot(datetime, Global_reactive_power, type="l", xlab="datetime"))
dev.off()