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
35 lines (30 loc) · 1.69 KB
/
Copy pathplot4.R
File metadata and controls
35 lines (30 loc) · 1.69 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
#############################
# file: plot4.R
# author: Bryan Wise
# Create plot 4 for project 1 of Exploratory data analysis
#############################
#Set working dir (env specific) to read in file
setwd("~/Presentations/Coursera/Exploratory Data Analysis/Project 1")
#load libraries used for data manipulation
library(sqldf)
library(lubridate)
#Read in just the data for the two days specified
all_data <- read.csv.sql("household_power_consumption.txt", header = TRUE, sep = ";", sql = "SELECT * from file where Date in ('1/2/2007','2/2/2007')")
#Create new datetime column that concatenates the date and the time columns
all_data$datetime <- dmy_hms(paste(all_data$Date,all_data$Time, sep = " ") )
#Generate the plot spcecified in the project
#Writes to current working directory
png("plot4.png", width = 480, height = 480, units = "px")
par(mfrow = c(2,2), mar = c(5,4,2,1), ps=12)
with(all_data, plot(datetime, Global_active_power, ylab = "Global Active Power", type = "n", xlab = " "))
with(all_data, lines(datetime, Global_active_power))
with(all_data, plot(datetime, Voltage, ylab = "Voltage", type = "n"))
with(all_data, lines(datetime, Voltage))
with(all_data, plot(datetime, Sub_metering_1, type = "n", ylab = "Energy sub metering", xlab =" "))
with(all_data, lines(datetime, Sub_metering_1))
with(all_data, lines(datetime, Sub_metering_2, col = "red"))
with(all_data, lines(datetime, Sub_metering_3, col = "blue"))
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lwd = 1, bty = "n")
with(all_data, plot(datetime, Global_reactive_power, ylab = "Global_reactive_power", type = "n"))
with(all_data, lines(datetime, Global_reactive_power))
dev.off()