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
55 lines (47 loc) · 1.89 KB
/
plot4.R
File metadata and controls
55 lines (47 loc) · 1.89 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zipfile <- "exdata_data_household_power_consumption.zip"
if (!file.exists(zipfile)) download.file(url, zipfile, method="curl")
file <- "household_power_consumption.txt"
if (!file.exists(file)) unzip(zipfile)
# TODO: could we read directly from the zipped file?
# how to select rows during loading?
# and without being particular about Date formatting?
data <- read.csv(file, sep=";", na.strings="?")
data <- data[as.Date(data$Date, "%d/%m/%Y")==as.Date("2007-02-01") |
as.Date(data$Date, "%d/%m/%Y")==as.Date("2007-02-02") ,]
png(file = "plot4.png")
par(mfcol=c(2,2))
plot(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Global_active_power),
type = "l",
xlab = "",
ylab = "Global Active Power (kilowatts)")
plot(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Sub_metering_1),
type = "n",
xlab = "",
ylab = "Energy sub metering")
lines(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Sub_metering_1),
col = "black")
lines(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Sub_metering_2),
col = "red")
lines(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Sub_metering_3),
col = "blue")
legend("topright",
lty=c(1,1,1),
col = c("black", "red", "blue"),
legend = paste("Sub_metering", c("1", "2", "3"), sep="_"))
plot(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Voltage),
type = "l",
xlab = "datetime",
ylab = "Voltage")
plot(strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S"),
as.numeric(data$Global_reactive_power),
type = "l",
xlab = "datetime",
ylab = "Global_reactive_power")
dev.off()