forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
43 lines (35 loc) · 1.3 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
get_data <- function() {
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
, "household_power_consumption.zip", method = "wget", extra = "-c")
unzip("household_power_consumption.zip")
t <- read.csv("household_power_consumption.txt", header = TRUE, sep = ";"
, na.strings = "?")
t[t$Date == "1/2/2007" | t$Date == "2/2/2007", ]
}
x <- get_data()
x$DT <- strptime(paste(as.character(x$Date),as.character(x$Time), sep = " ")
, format = "%d/%m/%Y %H:%M:%S")
Sys.setlocale(category = "LC_ALL", locale = "en_US")
png("plot4.png", 480, 480)
par(mfrow = c(2, 2))
# first
plot(x$DT, x$Global_active_power, type = "l"
, ylab = "Global Active Power", xlab = "")
#second
plot(x$DT, x$Voltage, type = "l"
, ylab = "Voltage", xlab = "datetime")
# third
plot(x$DT, x$Sub_metering_1, type = "n", xlab = ""
, ylab = "Energy sub metering")
lines(x$DT, x$Sub_metering_1, col = "black")
lines(x$DT, x$Sub_metering_2, col = "red")
lines(x$DT, x$Sub_metering_3, col = "blue")
legend("topright"
, legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
, col = c("black", "red", "blue")
, lty = 1
, bty = "n")
#forth
plot(x$DT, x$Global_reactive_power, type = "l"
, ylab = "Global_reactive_power", xlab = "datetime")
dev.off()