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
81 lines (59 loc) · 2.65 KB
/
plot4.R
File metadata and controls
81 lines (59 loc) · 2.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
#############################################################################
# Exploratory Data Analysis
# John Hopkins University
# Coursera Course Project
# Author: Anand Rao
# Date: 06 Sep 2014
# Project #1
##############################################################################
require(sqldf)
setwd("~/My Courses/ExploratoryDataAnalysis/Projects")
## Read Data for just the dates 1/2/2007 and 2/2/2007
data <- read.csv.sql( file='household_power_consumption.txt',
sep=";",
sql="select * from file where Date = '1/2/2007' or Date = '2/2/2007'",
header=TRUE)
data$datetime <- as.POSIXct(paste(data$Date, data$Time), format = "%d/%m/%Y %T")
## Open PNG device and create a plot "Plot2" in working directory with
## Width of 480 pixels and height of 480 pixels
png(filename="Plot4.png", width=480, height=480)
par(mfrow=c(2,2))
### Create the first plot
## Create a base plot frame with a new Y-axis label and X-axis label
with(data, plot(datetime, Global_active_power,
xlab="",
ylab = "Global Active Power",
type="n"))
## Draw the lines for the graph
with(data, lines(datetime, Global_active_power))
### Create the second plot
## Create a base plot frame with a new Y-axis label and X-axis label
with(data, plot(datetime, Voltage,
ylab = "Voltage",
type="n"))
## Draw the lines for the graph
with(data, lines(datetime, Voltage))
### Create the third plot
## Create a base plot frame with a new Y-axis label and X-axis label
with(data, plot(datetime, Sub_metering_1,
xlab="",
ylab = "Energy sub metering",
type="n"))
## Draw the line for Sub_metering_1 in Black color
with(data, lines(datetime, Sub_metering_1, col="black"))
## Draw the line for Sub_metering_2 in Red color
with(data, lines(datetime, Sub_metering_2, col="red"))
## Draw the line for Sub_metering_3 in Blue color
with(data, lines(datetime, Sub_metering_3, col="blue"))
legend("topright", lty=1,
col=c("black", "red", "blue"),
bty="n",
legend = c("Sub_metering_1","Sub_metering_2", "Sub_metering_3"))
### Create the fourth plot
## Create a base plot frame with a new Y-axis label and X-axis label
with(data, plot(datetime, Global_reactive_power,
type="n"))
## Draw the lines for the graph
with(data, lines(datetime, Global_reactive_power))
## Close the PNG file device
dev.off()