Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusglezalv committed Feb 19, 2021
1 parent 73fe5c6 commit b8d70a9
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
13 changes: 13 additions & 0 deletions ExData_Plotting1.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
24 changes: 24 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#load data.table library
library("data.table")

# Read file
householdEPC <- data.table::fread(input = "../data/household_power_consumption.txt", na.strings="?")

# Convert values to numeric
householdEPC[, Global_active_power := lapply(.SD, as.numeric), .SDcols = c("Global_active_power")]

# Parse date
householdEPC[, Date := lapply(.SD, as.Date, "%d/%m/%Y"), .SDcols = c("Date")]

# Subset observations from 2007-02-01 to 2007-02-02
householdEPC <- householdEPC[(Date >= "2007-02-01") & (Date <= "2007-02-02")]

#Open de graphic device of type PNG
png("plot1.png", width=480, height=480)

## Plot 1
hist(householdEPC[, Global_active_power], main="Global Active Power",
xlab="Global Active Power (kilowatts)", ylab="Frequency", col="Red")

# Close the device to save the file
dev.off()
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.
23 changes: 23 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#load data.table library
library("data.table")

# Read file
householdEPC <- data.table::fread(input = "../data/household_power_consumption.txt", na.strings="?")

# Convert values to numeric
householdEPC[, Global_active_power := lapply(.SD, as.numeric), .SDcols = c("Global_active_power")]

#Convert to POSIXct to use week days
householdEPC[, dateTime := as.POSIXct(paste(Date, Time), format = "%d/%m/%Y %H:%M:%S")]

# Subset observations from 2007-02-01 to 2007-02-02
householdEPC <- householdEPC[(dateTime >= "2007-02-01") & (dateTime < "2007-02-03")]

png("plot2.png", width=480, height=480)

## Plot 2
plot(x = householdEPC[, dateTime]
, y = householdEPC[, Global_active_power]
, type="l", xlab="", ylab="Global Active Power (kilowatts)")

dev.off()
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.
27 changes: 27 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#load data.table library
library("data.table")

# Read file
householdEPC <- data.table::fread(input = "../data/household_power_consumption.txt", na.strings="?")

# Convert values to numeric
householdEPC[, Global_active_power := lapply(.SD, as.numeric), .SDcols = c("Global_active_power")]

#Convert to POSIXct to use week days
householdEPC[, dateTime := as.POSIXct(paste(Date, Time), format = "%d/%m/%Y %H:%M:%S")]

# Subset observations from 2007-02-01 to 2007-02-02
householdEPC <- householdEPC[(dateTime >= "2007-02-01") & (dateTime < "2007-02-03")]

png("plot3.png", width=480, height=480)

# Plot 3
plot(householdEPC[, dateTime], householdEPC[, Sub_metering_1], type="l", xlab="", ylab="Energy sub metering")
lines(householdEPC[, dateTime], householdEPC[, Sub_metering_2],col="red")
lines(householdEPC[, dateTime], householdEPC[, Sub_metering_3],col="blue")
legend("topright"
, col=c("black","red","blue")
, c("Sub_metering_1 ","Sub_metering_2 ", "Sub_metering_3 ")
,lty=c(1,1), lwd=c(1,1))

dev.off()
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.
40 changes: 40 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#load data.table library
#load data.table library
library("data.table")

# Read file
householdEPC <- data.table::fread(input = "../data/household_power_consumption.txt", na.strings="?")

# Convert values to numeric
householdEPC[, Global_active_power := lapply(.SD, as.numeric), .SDcols = c("Global_active_power")]

#Convert to POSIXct to use week days
householdEPC[, dateTime := as.POSIXct(paste(Date, Time), format = "%d/%m/%Y %H:%M:%S")]

# Subset observations from 2007-02-01 to 2007-02-02
householdEPC <- householdEPC[(dateTime >= "2007-02-01") & (dateTime < "2007-02-03")]

png("plot4.png", width=480, height=480)

par(mfrow=c(2,2))

# Plot 1
plot(householdEPC[, dateTime], householdEPC[, Global_active_power], type="l", xlab="", ylab="Global Active Power")

# Plot 2
plot(householdEPC[, dateTime],householdEPC[, Voltage], type="l", xlab="datetime", ylab="Voltage")

# Plot 3
plot(householdEPC[, dateTime], householdEPC[, Sub_metering_1], type="l", xlab="", ylab="Energy sub metering")
lines(householdEPC[, dateTime], householdEPC[, Sub_metering_2], col="red")
lines(householdEPC[, dateTime], householdEPC[, Sub_metering_3],col="blue")
legend("topright", col=c("black","red","blue")
, c("Sub_metering_1 ","Sub_metering_2 ", "Sub_metering_3 ")
, lty=c(1,1)
, bty="n"
, cex=.5)

# Plot 4
plot(householdEPC[, dateTime], householdEPC[,Global_reactive_power], type="l", xlab="datetime", ylab="Global_reactive_power")

dev.off()
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 b8d70a9

Please sign in to comment.