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
92 lines (72 loc) · 2.48 KB
/
Copy pathplot4.R
File metadata and controls
92 lines (72 loc) · 2.48 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
setwd("~/ExData_Plotting1")
downloadURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
if (!dir.exists("Data")) {
dir.create("Data")
}
downloadFile <- "./Data/household_power_consumption.zip"
householdFile <- "./Data/household_power_consumption.txt"
if (!file.exists(householdFile)) {
download.file(downloadURL, downloadFile, method = "curl", mode = "wb")
unzip(downloadFile, overwrite = TRUE, exdir = "./Data")
}
plotData <- read.table(
householdFile,
header = TRUE,
sep = ";",
na.strings = "?",
stringsAsFactors = FALSE
)
finalData <- plotData[plotData$Date %in% c("1/2/2007", "2/2/2007"), ]
SetTime <- strptime(
paste(finalData$Date, finalData$Time, sep = " "),
"%d/%m/%Y %H:%M:%S"
)
finalData <- cbind(SetTime, finalData)
labels <- c("Sub_metering_1","Sub_metering_2","Sub_metering_3") columnlines <- c("black","red","blue") par(mfrow=c(2,2)) plot(finalData$SetTime, finalData$Global_active_power, type="l", col="green", xlab="", ylab="Global Active Power") plot(finalData$SetTime, finalData$Voltage, type="l", col="orange", xlab="datetime", ylab="Voltage") plot(finalData$SetTime, finalData$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering") lines(finalData$SetTime, finalData$Sub_metering_2, type="l", col="red") lines(finalData$SetTime, finalData$Sub_metering_3, type="l", col="blue") legend("topright", bty="n", legend=labels, lty=1, col=columnlines) plot(finalData$SetTime, finalData$Global_reactive_power, type="l", col="blue", xlab="datetime", ylab="Global_reactive_power")
png("plot4.png", width = 480, height = 480)
labels <- c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
columnlines <- c("black", "red", "blue")
par(mfrow = c(2, 2))
plot(
finalData$SetTime,
finalData$Global_active_power,
type = "l",
col = "black",
xlab = "",
ylab = "Global Active Power"
)
plot(
finalData$SetTime,
finalData$Voltage,
type = "l",
col = "black",
xlab = "datetime",
ylab = "Voltage"
)
plot(
finalData$SetTime,
finalData$Sub_metering_1,
type = "l",
col = "black",
xlab = "",
ylab = "Energy sub metering"
)
lines(finalData$SetTime, finalData$Sub_metering_2, col = "red")
lines(finalData$SetTime, finalData$Sub_metering_3, col = "blue")
legend(
"topright",
bty = "n",
legend = labels,
lty = 1,
col = columnlines
)
plot(
finalData$SetTime,
finalData$Global_reactive_power,
type = "l",
col = "black",
xlab = "datetime",
ylab = "Global_reactive_power"
)
dev.off()
file.exists("plot4.png")