diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..8440ab3567b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata +Data +ExData_Plotting1.Rproj \ No newline at end of file diff --git a/plot1.R b/plot1.R new file mode 100644 index 00000000000..a5400f89439 --- /dev/null +++ b/plot1.R @@ -0,0 +1,25 @@ +# download and read the data +url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(url = url, destfile = "Data/ElectricPowerConsumption.zip", mode = 'wb') + +dir.create("Data") +setwd("Data") +unzip(zipfile = "ElectricPowerConsumption.zip") +setwd ("..") + +data <- read.table("Data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = '?') + +# select only the data from 2007-02-01 and 2007-02-02 +data$Date <- as.Date(data$Date, format = "%d/%m/%Y") +data <- data[(data$Date >= "2007-02-01") & (data$Date <= "2007-02-02"), ] + +# convert the variable Global active power to data type numeric +data$Global_active_power <- as.numeric(data$Global_active_power) + +png(filename = "plot1.png", width = 480, height = 480, units = "px") +hist(data$Global_active_power, col = "red", main = "Global Active Power", + xlab = "Global Active Power (kilowatts)") +dev.off() + + + diff --git a/plot1.png b/plot1.png new file mode 100644 index 00000000000..db485ff3905 Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 00000000000..c5d63164bfe --- /dev/null +++ b/plot2.R @@ -0,0 +1,25 @@ +# download and read the data +url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(url = url, destfile = "Data/ElectricPowerConsumption.zip", mode = 'wb') + +dir.create("Data") +setwd("Data") +unzip(zipfile = "ElectricPowerConsumption.zip") +setwd ("..") + +data <- read.table("Data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = '?') + +# select only the data from 2007-02-01 and 2007-02-02 +data$DateTime <- as.POSIXct(paste(data$Date, data$Time), format = "%d/%m/%Y %H:%M:%S") +data <- data[(data$DateTime >= "2007-02-01") & (data$DateTime < "2007-02-03"), ] + + +# convert the variable Global active power to data type numeric +data$Global_active_power <- as.numeric(data$Global_active_power) + + +png(filename = "plot2.png", width = 480, height = 480, units = "px") +plot(x = data$DateTime, y = data$Global_active_power, pch = ".", xlab = "" + , ylab = "Global Active Power (kilowatts)") +lines(data$DateTime, data$Global_active_power) +dev.off() diff --git a/plot2.png b/plot2.png new file mode 100644 index 00000000000..841c4b4a7b3 Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 00000000000..e6e1fc5a050 --- /dev/null +++ b/plot3.R @@ -0,0 +1,25 @@ +# download and read the data +url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(url = url, destfile = "Data/ElectricPowerConsumption.zip", mode = 'wb') + +dir.create("Data") +setwd("Data") +unzip(zipfile = "ElectricPowerConsumption.zip") +setwd ("..") + +data <- read.table("Data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = '?') + +# select only the data from 2007-02-01 and 2007-02-02 +data$DateTime <- as.POSIXct(paste(data$Date, data$Time), format = "%d/%m/%Y %H:%M:%S") +data <- data[(data$DateTime >= "2007-02-01") & (data$DateTime < "2007-02-03"), ] + + +png(filename = "plot3.png", width = 480, height = 480, units = "px") +plot(x = data$DateTime, y = data$Sub_metering_1, pch = ".", xlab = "", + ylab = "Energy sub metering") +lines(x = data$DateTime, y = data$Sub_metering_1, col = "black") +lines(x = data$DateTime, y = data$Sub_metering_2, col = "red") +lines(x = data$DateTime, y = data$Sub_metering_3, col = "blue") +legend("topright", col = c("black", "red", "blue"), lty = c(1, 1), + legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")) +dev.off() diff --git a/plot3.png b/plot3.png new file mode 100644 index 00000000000..dd77d84c251 Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 00000000000..1f68633826e --- /dev/null +++ b/plot4.R @@ -0,0 +1,44 @@ +# download and read the data +url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(url = url, destfile = "Data/ElectricPowerConsumption.zip", mode = 'wb') + +dir.create("Data") +setwd("Data") +unzip(zipfile = "ElectricPowerConsumption.zip") +setwd ("..") + +data <- read.table("Data/household_power_consumption.txt", sep = ";", header = TRUE, na.strings = '?') + +# select only the data from 2007-02-01 and 2007-02-02 +data$DateTime <- as.POSIXct(paste(data$Date, data$Time), format = "%d/%m/%Y %H:%M:%S") +data <- data[(data$DateTime >= "2007-02-01") & (data$DateTime < "2007-02-03"), ] + + +png(filename = "plot4.png", width = 480, height = 480, units = "px") +# set the number of plots per row +par(mfrow = c(2, 2), mar = c(4, 4, 2, 1)) +# plot 1 +plot(x = data$DateTime, y = data$Global_active_power, pch = ".", xlab = "" + , ylab = "Global Active Power (kilowatts)") +lines(data$DateTime, data$Global_active_power) + +# plot 2 +plot(x = data$DateTime, y = data$Voltage, pch = ".", xlab = "datetime" + , ylab = "Voltage") +lines(data$DateTime, data$Voltage) + +# plot 3 +plot(x = data$DateTime, y = data$Sub_metering_1, pch = ".", xlab = "", + ylab = "Energy sub metering") +lines(x = data$DateTime, y = data$Sub_metering_1, col = "black") +lines(x = data$DateTime, y = data$Sub_metering_2, col = "red") +lines(x = data$DateTime, y = data$Sub_metering_3, col = "blue") +legend("topright", col = c("black", "red", "blue"), lty = c(1, 1), + legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), bty = "n", cex = .5) + +# plot 4 + +plot(x = data$DateTime, y = data$Global_reactive_power, pch = ".", xlab = "datetime" + , ylab = "Global Reactive Power") +lines(data$DateTime, data$Global_reactive_power) +dev.off() diff --git a/plot4.png b/plot4.png new file mode 100644 index 00000000000..66b40c9c19c Binary files /dev/null and b/plot4.png differ