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
96 lines (63 loc) · 1.65 KB
/
plot4.R
File metadata and controls
96 lines (63 loc) · 1.65 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
93
94
95
96
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running
loadData <- function(){
lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]
t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)
t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")
t
}
drawPlot1 <- function(tbl){
plot(
tbl$DateTime,
tbl$Global_active_power,
type="n",
ylab="Global Active Power",
xlab="")
lines(tbl$DateTime, tbl$Global_active_power)
}
drawPlot2 <- function(tbl){
plot(
tbl$DateTime,
tbl$Voltage,
type="n",
ylab="Voltage",
xlab="datetime")
lines(tbl$DateTime, tbl$Voltage)
}
drawPlot3 <- function(tbl){
plot(
tbl$DateTime,
tbl$Sub_metering_1,
type="n",
ylab="Energy sub metering",
xlab="")
lines(tbl$DateTime, tbl$Sub_metering_1, col="black")
lines(tbl$DateTime, tbl$Sub_metering_2, col="red")
lines(tbl$DateTime, tbl$Sub_metering_3, col="blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lwd = 1)
}
drawPlot4 <- function(tbl){
plot(
tbl$DateTime,
tbl$Global_reactive_power,
type="n",
ylab="Global_reactive_power",
xlab="datetime")
lines(tbl$DateTime, tbl$Global_reactive_power)
}
t <- loadData()
png(filename = "plot4.png")
par(mfrow=c(2,2))
drawPlot1(t)
drawPlot2(t)
drawPlot3(t)
drawPlot4(t)
dev.off()