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
55 lines (44 loc) · 1.47 KB
/
plot4.R
File metadata and controls
55 lines (44 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
49
50
51
52
53
54
55
library(sqldf)
# read data from file
hpc <- read.csv.sql("../data/household_power_consumption.txt", sep=";",
sql="SELECT *
FROM file
WHERE Date IN ('1/2/2007','2/2/2007')")
#convert ?'s to NA's
hpc[hpc=="?"]=NA
# add datetime column
hpc$DateTime <- as.POSIXct(paste(hpc$Date, hpc$Time),
format="%d/%m/%Y %H:%M:%S")
# 4 subplots
par(mfrow = c(2,2))
# create plot 1
with(hpc, plot(DateTime, Global_active_power,
type="l",
ylab="Global Active Power",
xlab=""))
# create plot 2
with(hpc, plot(DateTime, Voltage,
type="l",
ylab="Voltage",
xlab="datetime"))
# create plot 3
with(hpc, plot(DateTime,
Sub_metering_1,
type="l",
ylab="Energy sub metering",
xlab=""))
with(hpc, lines(DateTime, Sub_metering_2, col="red"))
with(hpc, lines(DateTime, Sub_metering_3, col="blue"))
legend("topright",
lty=c(1,1,1),
bty="n",
col=c("black", "red", "blue"),
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
# create plot 4
with(hpc, plot(DateTime, Global_reactive_power,
type="l",
ylab="Global_reactive_power",
xlab="datetime"))
# save graphic to file
dev.copy(png, filename="plot4.png", width=480, height=480)
dev.off()