forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
60 lines (51 loc) · 1.31 KB
/
plot4.R
File metadata and controls
60 lines (51 loc) · 1.31 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
## (there are only two days, so date alone wouldn't produce sufficient data')
## we use a combination of date and time
## first convert character to date ...
data$Date <- strptime(data$Date, "%d/%m/%Y")
## ... then add time to it (using strptime does not work),
## creating a new combination Moments
data$Moments <- as.POSIXct(paste(data$Date, data$Time))
png("plot2.png", width=480, height= 480) # open png device
par(mfrow = c(2,2)) # 2x2 plots, row-wise
# row 1, col 1
plot(data$Moments,
data$Global_active_power,
type= "l",
ylab= "Global Active Power (kilowatts)",
xlab="")
# row 1, col 2
plot(data$Moments,
data$Voltage,
type= "l",
ylab= "Voltage",
xlab="datetime")
# row 2, col 1
plot(data$Moments,
data$Sub_metering_1,
type= "n",
ylab= "Energy sub metering",
xlab="")
points(data$Moments,
data$Sub_metering_1,
type= "l",
col="black")
points(data$Moments,
data$Sub_metering_2,
type= "l",
col="red")
points(data$Moments,
data$Sub_metering_3,
type= "l",
col="blue")
legend("topright",
c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
lty=c(1,1,1),
lwd=c(2.5,2.5,2.5),
col=c("black","red","blue"))
# row 2, col 2
plot(data$Moments,
data$Global_reactive_power,
type= "l",
ylab= "Global reactive power",
xlab="datetime")
dev.off() ## close device