Skip to content

Commit

Permalink
Added scripts and the images
Browse files Browse the repository at this point in the history
  • Loading branch information
piust committed Sep 26, 2018
1 parent 73fe5c6 commit 7c841b5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
50 changes: 50 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(dplyr)

## PLotting function
plot1 <- function(){
## Build the environment
build_environment()
## Read data
data <- read_dataset()
## Set the png device
png("plot1.png")
## Draw the Global_active_power histagram
with(data, hist(Global_active_power, col="red",xlab = "Global Active Power (kilowatts)",
main = "Global Active Power"))
## Close the device
dev.off()
}

## This function downloads and unzips the file needed by the project
build_environment <- function(){
## Directory to store the data
data_dir <- "data"
## The actual date
date <- Sys.Date()
## The URL containing the data
download_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## The file containing the data
data_file <- file.path(data_dir,"household_power_consumption.txt")
## The name og the zip file
zip_file <- file.path(data_dir,paste0("project_",date,".zip"))
## Creation of the working environment
if(!file.exists(data_dir)){dir.create(data_dir)}
if(!file.exists(zip_file)){
download.file(download_url, zip_file)
unzip(zip_file,exdir = data_dir)
}
}

## This function reads the dataset and filter by the requested date range.
## Il casts the Date variable to Date class and the Time variable to Time class
read_dataset <- function(){
## Read the dataset
data <- read.delim("data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = "?")
## Convert the Time column to Time class
data$Time <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
## Convert the Date column to Date class
data$Date <- as.Date(data$Date, format="%d/%m/%Y")
## Filter the rows by date
data <- data[data$Date <= "2007-02-02" & data$Date >= "2007-02-01",]
}

Binary file added plot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
library(dplyr)

## PLotting function
plot2 <- function(){
## Build the environment
build_environment()
## Read data
data <- read_dataset()
## Set the png device
png("plot2.png")
## Draw the Global_active_power graph
plot(x = data$Time, y=data$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power (kilowatts)")
## Close the device
dev.off()
}

## This function downloads and unzips the file needed by the project
build_environment <- function(){
## Directory to store the data
data_dir <- "data"
## The actual date
date <- Sys.Date()
## The URL containing the data
download_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## The file containing the data
data_file <- file.path(data_dir,"household_power_consumption.txt")
## The name og the zip file
zip_file <- file.path(data_dir,paste0("project_",date,".zip"))
## Creation of the working environment
if(!file.exists(data_dir)){dir.create(data_dir)}
if(!file.exists(zip_file)){
download.file(download_url, zip_file)
unzip(zip_file,exdir = data_dir)
}
}

## This function reads the dataset and filter by the requested date range.
## Il casts the Date variable to Date class and the Time variable to Time class
read_dataset <- function(){
## Read the dataset
data <- read.delim("data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = "?")
## Convert the Time column to Time class
data$Time <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
## Convert the Date column to Date class
data$Date <- as.Date(data$Date, format="%d/%m/%Y")
## Filter the rows by date
data <- data[data$Date <= "2007-02-02" & data$Date >= "2007-02-01",]
}

Binary file added plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(dplyr)

## PLotting function
plot3 <- function(){
## Build the environment
build_environment()
## Read data
data <- read_dataset()
## Set the png device
png("plot3.png")
## Draw the Energy sub metering graphs
plot(x = data$Time, y=data$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering", col="black")
with(data, lines(x=Time, y=Sub_metering_2, col="red"))
with(data, lines(x=Time, y=Sub_metering_3, col="blue"))
## Add the legend
legend("topright",lwd=c(2.5,2.5), col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
## Close the device
dev.off()
}

## This function downloads and unzips the file needed by the project
build_environment <- function(){
## Directory to store the data
data_dir <- "data"
## The actual date
date <- Sys.Date()
## The URL containing the data
download_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## The file containing the data
data_file <- file.path(data_dir,"household_power_consumption.txt")
## The name og the zip file
zip_file <- file.path(data_dir,paste0("project_",date,".zip"))
## Creation of the working environment
if(!file.exists(data_dir)){dir.create(data_dir)}
if(!file.exists(zip_file)){
download.file(download_url, zip_file)
unzip(zip_file,exdir = data_dir)
}
}

## This function reads the dataset and filter by the requested date range.
## Il casts the Date variable to Date class and the Time variable to Time class
read_dataset <- function(){
## Read the dataset
data <- read.delim("data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = "?")
## Convert the Time column to Time class
data$Time <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
## Convert the Date column to Date class
data$Date <- as.Date(data$Date, format="%d/%m/%Y")
## Filter the rows by date
data <- data[data$Date <= "2007-02-02" & data$Date >= "2007-02-01",]
}

Binary file added plot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
library(dplyr)

## PLotting function
plot4 <- function(){
## Build the environment
build_environment()
## Read data
data <- read_dataset()
## Set the png device
png("plot4.png")
## Prepare to draw graphs in a 2+2 grid
par(mfcol=c(2,2))
## Draw the Global_active_power histagram
plot(x = data$Time, y=data$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power")
## Draw the Energy sub metering graphs
plot(x = data$Time, y=data$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering", col="black")
with(data, lines(x=Time, y=Sub_metering_2, col="red"))
with(data, lines(x=Time, y=Sub_metering_3, col="blue"))
## Add the legend
legend("topright",lwd=c(2.5,2.5), bty = "n", col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
## Draw the Voltage graph
plot(x = data$Time, y=data$Voltage, type = "l", xlab="datetime", ylab = "Voltage")
## Draw the Global reactive power graph
plot(x = data$Time, y=data$Global_reactive_power, type = "l", xlab="datetime", ylab = "Global_reactive_power")
## Close the device
dev.off()
}

## This function downloads and unzips the file needed by the project
build_environment <- function(){
## Directory to store the data
data_dir <- "data"
## The actual date
date <- Sys.Date()
## The URL containing the data
download_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## The file containing the data
data_file <- file.path(data_dir,"household_power_consumption.txt")
## The name og the zip file
zip_file <- file.path(data_dir,paste0("project_",date,".zip"))
## Creation of the working environment
if(!file.exists(data_dir)){dir.create(data_dir)}
if(!file.exists(zip_file)){
download.file(download_url, zip_file)
unzip(zip_file,exdir = data_dir)
}
}

## This function reads the dataset and filter by the requested date range.
## Il casts the Date variable to Date class and the Time variable to Time class
read_dataset <- function(){
## Read the dataset
data <- read.delim("data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = "?")
## Convert the Time column to Time class
data$Time <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
## Convert the Date column to Date class
data$Date <- as.Date(data$Date, format="%d/%m/%Y")
## Filter the rows by date
data <- data[data$Date <= "2007-02-02" & data$Date >= "2007-02-01",]
}

Binary file added plot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7c841b5

Please sign in to comment.