diff --git a/plot1.R b/plot1.R new file mode 100644 index 00000000000..a11acab3055 --- /dev/null +++ b/plot1.R @@ -0,0 +1,11 @@ +# Create Plot 1 +png("plot1.png", width = 480, height = 480) + +hist(data$Global_active_power, + col = "red", + main = "Global Active Power", + xlab = "Global Active Power (kilowatts)", + ylab = "Frequency") + +dev.off() + diff --git a/plot1.png b/plot1.png new file mode 100644 index 00000000000..2a299e733b4 Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 00000000000..74bfc59f68c --- /dev/null +++ b/plot2.R @@ -0,0 +1,17 @@ +# Load data +data <- read.table("household_power_consumption.txt", header=TRUE, sep=";", na.strings="?", stringsAsFactors=FALSE) +data$Date <- as.Date(data$Date, format="%d/%m/%Y") +data$DateTime <- strptime(paste(data$Date, data$Time), format="%Y-%m-%d %H:%M:%S") + +# Subset data +data_subset <- subset(data, Date >= as.Date("2007-02-01") & Date <= as.Date("2007-02-02")) + +# Create PNG file +png("plot2.png", width=480, height=480) + +# Create line plot +plot(data_subset$DateTime, data_subset$Global_active_power, type="l", + xlab="", ylab="Global Active Power (kilowatts)") + +# Close file +dev.off() diff --git a/plot2.png b/plot2.png new file mode 100644 index 00000000000..a57175afb3f Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 00000000000..98225e5691e --- /dev/null +++ b/plot3.R @@ -0,0 +1,25 @@ +# Load data +data <- read.table("household_power_consumption.txt", header=TRUE, sep=";", na.strings="?", stringsAsFactors=FALSE) +data$Date <- as.Date(data$Date, format="%d/%m/%Y") +data$DateTime <- strptime(paste(data$Date, data$Time), format="%Y-%m-%d %H:%M:%S") + +# Subset data +data_subset <- subset(data, Date >= as.Date("2007-02-01") & Date <= as.Date("2007-02-02")) + +# Create PNG file +png("plot3.png", width=480, height=480) + +# Create multi-line plot +plot(data_subset$DateTime, data_subset$Sub_metering_1, type="l", col="black", + xlab="", ylab="Energy sub metering") +lines(data_subset$DateTime, data_subset$Sub_metering_2, col="red") +lines(data_subset$DateTime, data_subset$Sub_metering_3, col="blue") + +# Add legend +legend("topright", + legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), + col=c("black", "red", "blue"), lty=1) + +# Close file +dev.off() + diff --git a/plot3.png b/plot3.png new file mode 100644 index 00000000000..7f6818dfc3d Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 00000000000..a1025f39f54 --- /dev/null +++ b/plot4.R @@ -0,0 +1,37 @@ +# Load data +data <- read.table("household_power_consumption.txt", header=TRUE, sep=";", na.strings="?", stringsAsFactors=FALSE) +data$Date <- as.Date(data$Date, format="%d/%m/%Y") +data$DateTime <- strptime(paste(data$Date, data$Time), format="%Y-%m-%d %H:%M:%S") + +# Subset data +data_subset <- subset(data, Date >= as.Date("2007-02-01") & Date <= as.Date("2007-02-02")) + +# Create PNG file +png("plot4.png", width=480, height=480) + +# Set up a 2x2 plot layout +par(mfrow=c(2,2)) + +# Plot 1 (Top left) - Global Active Power +plot(data_subset$DateTime, data_subset$Global_active_power, type="l", + xlab="", ylab="Global Active Power") + +# Plot 2 (Top right) - Voltage +plot(data_subset$DateTime, data_subset$Voltage, type="l", + xlab="datetime", ylab="Voltage") + +# Plot 3 (Bottom left) - Energy sub-metering +plot(data_subset$DateTime, data_subset$Sub_metering_1, type="l", col="black", + xlab="", ylab="Energy sub metering") +lines(data_subset$DateTime, data_subset$Sub_metering_2, col="red") +lines(data_subset$DateTime, data_subset$Sub_metering_3, col="blue") +legend("topright", legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), + col=c("black", "red", "blue"), lty=1, bty="n") + +# Plot 4 (Bottom right) - Global Reactive Power +plot(data_subset$DateTime, data_subset$Global_reactive_power, type="l", + xlab="datetime", ylab="Global Reactive Power") + +# Close file +dev.off() + diff --git a/plot4.png b/plot4.png new file mode 100644 index 00000000000..5e2836ee649 Binary files /dev/null and b/plot4.png differ