Skip to content

Commit 18a2cdd

Browse files
committed
Scripts for plots
1 parent 73fe5c6 commit 18a2cdd

File tree

10 files changed

+79
-0
lines changed

10 files changed

+79
-0
lines changed

LoadData.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
###### Load dataset into R ####################
2+
3+
######### Download the zip file in working area #########
4+
setwd("./ExporatoryDataAnalysis")
5+
url<-"https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
6+
file <- "household_power_consumption"
7+
download.file(url, file, method = "curl")
8+
9+
# Extract the contents of zip file, gives "household_power_consumption.txt" file
10+
unzip(file, exdir = "./")
11+
dataFile <- "household_power_consumption.txt"
12+
13+
#Load the dataset into data frame
14+
data <- read.table(dataFile, header=TRUE, sep=";")
15+
16+
# Subset the data for the two dates of interest
17+
dataSubset <- data[data$Date=="1/2/2007" | data$Date=="2/2/2007",]
18+
19+
# Convert columns (3-9) to numeric
20+
for(i in c(3:9)) {dataSubset[,i] <- as.numeric(as.character(dataSubset[,i]))}
21+
22+
# Create Date Time variable
23+
# Convert Date Time variable to proper format
24+
datetime <- strptime(paste(dataSubset$Date, dataSubset$Time, sep=" "), "%d/%m/%Y %H:%M:%S")
25+
26+
27+
28+
29+
30+
31+

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ The four plots that you will need to construct are shown below.
112112

113113
![plot of chunk unnamed-chunk-5](figure/unnamed-chunk-5.png)
114114

115+
116+
## Instructions for generating plots
117+
1. Download the scripts (LoadData.R, plot1.R, plot2.R, plot3.R, plot4.R) to some directory;
118+
2. source("LoadData.R") will download the zipped data file and extract the dataset
119+
and save in the working area.
120+
3. The scipts plot1.R, plot2.R, plot3.R and plot4.R generates and saves png files.

plot1.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Script for Plot 1
2+
## Uses information about the dataset and variables from the script LoadData.R
3+
## source("LoadData.R") will load the dataset and variables in R
4+
## source("plot1.R") will save the png file in the working area
5+
6+
png("plot1.png", width=480, height=480)
7+
hist(dataSubset$Global_active_power, col = "red", main = "Global Active Power", xlab = "Global Active Power(kilowatts)")
8+
dev.off()

plot1.png

19.1 KB
Loading

plot2.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Script for Plot 2
2+
## Uses information about the dataset and variables from script LoadData.R
3+
## source("LoadData.R") will load the dataset and variables in R
4+
## source("plot2.R") will save the png file in the working area
5+
6+
png("plot2.png", width=480, height=480)
7+
plot(datetime, dataSubset$global_active_power, type="l", xlab="", ylab="Global Active Power (kilowatts)")
8+
dev.off()

plot2.png

31.6 KB
Loading

plot3.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Script for Plot 3
2+
## Uses information about the dataset and variables from script LoadData.R
3+
## source("LoadData.R") will load the dataset and variables in R
4+
## source("plot3.R") will save the png file in the working area
5+
6+
png("plot3.png", width=480, height=480)
7+
plot(datetime, dataSubset$Sub_metering_1, type="l", ylab="Energy sub metering", xlab="")
8+
lines(datetime, dataSubset$Sub_metering_2, type="l", col="red")
9+
lines(datetime, dataSubset$Sub_metering_3, type="l", col="blue")
10+
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, lwd=2.5, col=c("black", "red", "blue"))
11+
dev.off()

plot3.png

26.3 KB
Loading

plot4.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Script for Plot 4
2+
## Uses information about the dataset and variables from script LoadData.R
3+
## source("LoadData.R") will load the dataset and variables in R
4+
## source("plot4.R") will save the png file in the working area
5+
6+
png("plot4.png", width=480, height=480)
7+
par(mfrow = c(2, 2))
8+
plot(datetime,dataSubset$Global_active_power, type="l", xlab="", ylab="Global Active Power")
9+
plot(datetime, dataSubset$Voltage, type="l", xlab="datetime", ylab="Voltage")
10+
plot(datetime, dataSubset$Sub_metering_1, type="l", ylab="Energy sub metering", xlab="")
11+
lines(datetime, dataSubset$Sub_metering_2, type="l", col="red")
12+
lines(datetime, dataSubset$Sub_metering_3, type="l", col="blue")
13+
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, lwd=2.5, col=c("black", "red", "blue"), bty="o")
14+
plot(datetime, dataSubset$Global_reactive_power, type="l", xlab="datetime", ylab="Global_reactive_power")
15+
dev.off()

plot4.png

53.7 KB
Loading

0 commit comments

Comments
 (0)