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
48 lines (37 loc) · 1.47 KB
/
plot4.R
File metadata and controls
48 lines (37 loc) · 1.47 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
power <- read.table("household_power_consumption.txt", sep = ";",
col.names = c("Date", "Time", "Global_active_power",
"Global_reactive_power", "Voltage",
"Global_intensity", "Sub_metering_1",
"Sub_metering_2", "Sub_metering_3"),
na.strings = "?", skip = 66600)
## use the right dates
relevant <- power[power$Date == "1/2/2007" | power$Date == "2/2/2007",]
## format date correctly
datetimes <- strptime(paste(relevant$Date, relevant$Time),
format = "%d/%m/%Y %H:%M:%S")
par(mfcol = c(2, 2))
## first plot
plot(datetimes, relevant$Global_active_power,
xlab = "", ylab = "Global Active Power",
type = "l")
## second plot
plot(datetimes, relevant$Sub_metering_1,
xlab = "", ylab = "Energy sub metering",
type = "l")
lines(datetimes, relevant$Sub_metering_2,
type = "l", col = "red")
lines(datetimes, relevant$Sub_metering_3,
type = "l", col = "blue")
legend(x = "topright",
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black", "red", "blue"), lty = 1, bty = "n")
## third plot
plot(datetimes, relevant$Voltage,
xlab = "datetime", ylab = "Voltage", type = "l")
## fourth plot
plot(datetimes, relevant$Global_reactive_power,
xlab = "datetime", ylab = "Global_reactive_power",
type = "l")
## write file
dev.copy(png, "plot4.png")
dev.off()