-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
76 lines (65 loc) · 2.8 KB
/
plot4.R
File metadata and controls
76 lines (65 loc) · 2.8 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
#---------------------------------------------------------------------------#
# Program: plot4.R #
# Author: Cindy Molitor #
# Creation Date: 29Apr2017 #
# Purpose: Create four graphs based on the household power consumption data #
# frame. #
# Project: Coursera Data Science program #
# Exploratory Data Analysis Week 1 programming assignment #
# #
# Inputs: household_power_consumption.txt #
# #
# Outputs: plot4.png #
#---------------------------------------------------------------------------#
#-------------------------------------------------------------------------#
# The file has over 2 million rows, so use sqldf to only read in the data #
# for the specified dates. #
#-------------------------------------------------------------------------#
library(sqldf)
hpc <- file('household_power_consumption.txt')
attr(hpc, "file.format") <- list(sep=';', header=TRUE)
hpc_df <- sqldf("select * from hpc where Date='1/2/2007' or Date='2/2/2007'")
#----------------------------------------------------------------#
# Add a column to the data frame for the combined date and time. #
#----------------------------------------------------------------#
hpc_df$date_time <- as.POSIXct(paste(hpc_df$Date, hpc_df$Time),
format="%d/%m/%Y %H:%M:%S")
png('plot4.png', width=480, height=480)
par(mfcol=c(2,2))
#-------------#
# First graph #
#-------------#
plot(hpc_df$date_time, hpc_df$Global_active_power,
type="l",
xlab="",
ylab="Global Active Power (kilowatts)")
#--------------#
# Second graph #
#--------------#
plot(hpc_df$date_time, hpc_df$Sub_metering_1,
type="l",
xlab="",
ylab="Energy sub metering")
lines(hpc_df$date_time, hpc_df$Sub_metering_2, col="red")
lines(hpc_df$date_time, hpc_df$Sub_metering_3, col="blue")
legend("topright",
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col=c("black","red","blue"),
lty=1,
bty="n")
#-------------#
# Third graph #
#-------------#
plot(hpc_df$date_time, hpc_df$Voltage,
type="l",
xlab="datetime",
ylab="Voltage")
#--------------#
# Fourth graph #
#--------------#
plot(hpc_df$date_time, hpc_df$Global_reactive_power,
type="l",
xlab="datetime",
ylab="Global_reactive_power")
dev.off()
#----- End of Program -----#