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
37 lines (25 loc) · 1.35 KB
/
Copy pathplot4.R
File metadata and controls
37 lines (25 loc) · 1.35 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
32
33
34
35
36
library(dplyr)
# check if the data file is already downloaded, if not, download and unzip
if(!file.exists("zipdata.zip")) {
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", destfile = "zipdata.zip")
unzip("zipdata.zip")}
data_full <- read.table("household_power_consumption.txt", header = T, sep = ";", na.strings = "?")
data <- filter(data_full, Date == "1/2/2007" | Date == "2/2/2007")
#remove large data frame
rm(data_full)
data <- mutate(data, DT = paste(Date, Time))
data$DT <- strptime(data$DT, format = "%d/%m/%Y %H:%M:%S")
png(filename = "plot4.png", height = 480, width = 480)
par(mfcol = c(2,2))
plot(data$DT, data$Global_active_power, xlab = "", ylab = "Global Active Power (kilowats)", type = "n")
lines(data$DT, data$Global_active_power)
plot(data$DT, data$Sub_metering_1, xlab = "", ylab = "Energy sub metering", type = "n")
with(data, lines(DT, Sub_metering_1))
with(data, lines(DT, Sub_metering_2, col = "red"))
with(data, lines(DT, Sub_metering_3, col = "blue"))
legend("topright", lty = 1, col = c("black", "red", "blue"), legend = colnames(data[7:9]), bty = "n")
with(data, plot(DT, Voltage, xlab = "datetime", type = "n"))
with(data, lines(DT, Voltage))
with(data, plot(DT, Global_reactive_power, xlab = "datetime", type = "n"))
with(data, lines(DT, Global_reactive_power))
dev.off()