forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
64 lines (53 loc) · 1.27 KB
/
plot4.R
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
# read data
data_ <- read.table("household_power_consumption.txt", sep = ";", na.strings = "?", header = T);
# Convert date and time
data_$Time <- strptime(x = paste(data_$Date, data_$Time, sep = " "), format = "%d/%m/%Y %H:%M:%S")
data_$Date <- as.Date(data_$Date,format = '%d/%m/%Y')
start_ <- as.Date("2007-02-01")
end_ <- as.Date("2007-02-02")
data2 <- data_[which(data_$Date >= start_ & data_$Date <= end_),]
# draw plot 4
png(file = "plot4.png", width = 480, height = 480, units = "px")
par(mfrow=c(2,2))
# 1
plot(
x = data2$Time,
y = data2$Global_active_power,
type = "l",
xlab="",
ylab="Global Active Power"
)
# 2
plot(
x = data2$Time,
y = data2$Voltage,
type = "l",
xlab="datetime",
ylab="Voltage"
)
# 3
plot(
x = data2$Time,
y = data2$Sub_metering_1,
type = "l",
xlab="",
ylab="Energy sub metering"
)
lines(
x=data2$Time,
y=data2$Sub_metering_2,
col="red")
lines(
x=data2$Time,
y=data2$Sub_metering_3,
col="blue")
legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),bty="l",col=c("black","red","blue"),lwd=2,cex=0.7)
# 4
plot(
x = data2$Time,
y = data2$Global_reactive_power,
type = "l",
xlab="datetime",
ylab="Global_reactive_power"
)
dev.off()