Skip to content

Commit 0e8dd85

Browse files
C4W1 Course Project 1 files
1 parent 73fe5c6 commit 0e8dd85

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-0
lines changed

getPlotData.R

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# By Richard Wheeler
2+
# Script for C4W1 project
3+
# getPlotData.R
4+
5+
getPlotData <- function() {
6+
inputfile <- file("household_power_consumption.txt", open="rt")
7+
outputfile <- file("output.txt", open = "wt")
8+
colnames <- readLines(inputfile,n=1, warn = FALSE)
9+
colnames <- unlist(strsplit(colnames,split = ";"))
10+
colnames <- c(paste0(colnames[1], colnames[2],collaspe=""), colnames[3:9])
11+
while(length(data <- readLines(inputfile,n=1000, warn = FALSE)) > 0) {
12+
for (i in 1:length(data)) {
13+
if(any(sapply(as.Date(strsplit(data[i],";")[[1]][1],"%d/%m/%Y"),grepl,"2007-02-01 2007-02-02"))) {
14+
dline <- paste(format(as.Date(strsplit(data[i],";")[[1]][1],"%d/%m/%Y"),"%Y-%m-%d"),paste(strsplit(data[i],";")[[1]][2:9], collapse = ';'),sep = " ")
15+
writeLines(dline,outputfile)
16+
}# Write only data at the sepecified dates
17+
}
18+
if(as.Date(strsplit(data[i],";")[[1]][1],"%d/%m/%Y") >= "2007-02-03") break
19+
}#while LOOP
20+
close(inputfile)
21+
close(outputfile)
22+
23+
data <- read.table("output.txt",sep=';',header = FALSE, col.names = colnames, blank.lines.skip = TRUE, stringsAsFactors = FALSE)
24+
return(data)
25+
}#getPlotData()

plot1.R

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# By Richard Wheeler
2+
# Script for C4W1 project
3+
# plot1.R
4+
5+
plot1 <- function() {
6+
if(!exists("getPlotData", mode="function")) source("getPlotData.R")
7+
pdata <- getPlotData()
8+
hist(pdata$Global_active_power, breaks = 36, xaxt='n', col = "red",
9+
main = "Global Active Power",
10+
xlab = "Global Active Power (kilowatts)",
11+
ylim = c(0,1200))
12+
axis(side = 1, at=seq(0,6,2), labels=seq(0,6,2))
13+
dev.copy(png,file="plot1.png")
14+
dev.off()
15+
}#plot1()

plot1.png

19.8 KB
Loading

plot2.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# By Richard Wheeler
2+
# Script for C4W1 project
3+
# plot2.R
4+
5+
if(!exists("getPlotData", mode="function")) source("getPlotData.R")
6+
7+
plot2 <- function(dispOnly = FALSE, pdata = NULL) {
8+
if(dispOnly == FALSE) pdata <- getPlotData()
9+
10+
plot.ts(pdata$Global_active_power, axes=FALSE, ann=FALSE)
11+
box()
12+
title(ylab="Global Active Power (Kilowatts)")
13+
axis(2, at=c(0,2,4,6))
14+
axis(1,at=c(0,1440,2880),labels=c("Thu","Fri","Sat"))
15+
if(dispOnly == FALSE) {
16+
dev.copy(png,file="plot2.png")
17+
dev.off()
18+
}
19+
}#plot2()

plot2.png

31.6 KB
Loading

plot3.R

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# By Richard Wheeler
2+
# Script for C4W1 project
3+
# plot3.R
4+
5+
if(!exists("getPlotData", mode="function")) source("getPlotData.R")
6+
7+
plot3 <- function(dispOnly = FALSE, pdata = NULL) {
8+
boxtype <- "o"
9+
textwidth <- 600
10+
if(dispOnly == FALSE) {
11+
pdata <- getPlotData()
12+
} else {
13+
boxtype <- "n"
14+
textwidth <- 1050
15+
}
16+
17+
plot(pdata$Sub_metering_1, type="n", xaxt="n", ylab="Energy sub metering")
18+
lines(pdata$Sub_metering_1,type="l")
19+
lines(pdata$Sub_metering_2,type="l",col="red")
20+
lines(pdata$Sub_metering_3,type="l",col="blue")
21+
axis(1,at=c(0,1440,2880),labels=c("Thu","Fri","Sat"))
22+
legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),col=c("black","red","blue"),lty=1,
23+
text.width = textwidth, cex=.75, y.intersp = .75, bty = boxtype)
24+
25+
if(dispOnly == FALSE) {
26+
dev.copy(png,file="plot3.png")
27+
dev.off()
28+
}
29+
}#plot3()

plot3.png

25.9 KB
Loading

plot4.R

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# By Richard Wheeler
2+
# Script for C4W1 project
3+
# plot4.R
4+
5+
if(!exists("getPlotData", mode ="function")) source("getPlotData.R")
6+
if(!exists("plot2", mode = "function")) source("plot2.R")
7+
if(!exists("plot3", mode = "function")) source("plot3.R")
8+
9+
plot4 <- function() {
10+
par(mfrow=c(2,2))
11+
pdata <- getPlotData()
12+
plot2(TRUE, pdata)
13+
14+
plot(pdata$Voltage, type="n", xaxt="n", ylab="Voltage", xlab = "datetime")
15+
lines(pdata$Voltage,type="l")
16+
axis(1,at=c(0,1440,2880),labels=c("Thu","Fri","Sat"))
17+
#text(1, labels = "datetime", cex = 1)
18+
19+
plot3(TRUE, pdata)
20+
21+
plot(pdata$Global_reactive_power, type="n", xaxt="n", ylab="Global Reactive Power", xlab = "datetime")
22+
lines(pdata$Global_reactive_power,type="l")
23+
axis(1,at=c(0,1440,2880),labels=c("Thu","Fri","Sat"))
24+
25+
dev.copy(png,file="plot4.png")
26+
dev.off()
27+
}#plot4()

plot4.png

54.6 KB
Loading

0 commit comments

Comments
 (0)