Skip to content

Commit

Permalink
Adding scripts to generate plots and generated PNGs
Browse files Browse the repository at this point in the history
  • Loading branch information
Manish Shah authored and Manish Shah committed Nov 30, 2022
1 parent 73fe5c6 commit abb6811
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 0 deletions.
23 changes: 23 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Load the data
household_power <- read.csv("household_power_consumption.txt", sep = ";")

# Convert Date strings to Date object
household_power$Date <- as.Date(household_power$Date, format="%d/%m/%Y")

# Convert Global Active Power to numeric values
household_power$Global_active_power <- as.numeric(household_power$Global_active_power)

# Filter to data from 2007-02-01 and 2007-02-02
d <- rbind(subset(household_power, household_power$Date == as.Date("2007-02-01")),
subset(household_power, household_power$Date == as.Date("2007-02-02")))

# Make the histogram plot with the filtered data
hist(d$Global_active_power,
main="Global Active Power",
col="red",
xlab="Global Active Power (kilowatts)",
breaks=24)

# Export plot to PNG
dev.copy(png, file="plot1.png")
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.
30 changes: 30 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Load the data
household_power <- read.csv("household_power_consumption.txt", sep = ";")

# Add DateTime field
household_power$DateTime <- paste(household_power$Date, household_power$Time)

# Convert Date strings to Date object
household_power$Date <- as.Date(household_power$Date, format="%d/%m/%Y")

# Convert Time strings to Time object
household_power$Time <- strptime(household_power$DateTime,
format="%d/%m/%Y %H:%M:%OS")

# Convert Global Active Power to numeric values
household_power$Global_active_power <- as.numeric(household_power$Global_active_power)

# Filter to data from 2007-02-01 and 2007-02-02
d <- rbind(subset(household_power, household_power$Date == as.Date("2007-02-01")),
subset(household_power, household_power$Date == as.Date("2007-02-02")))

# Create the line graph
plot(d$Time,
d$Global_active_power,
type="l",
ylab="Global Active Power (kilowatts)",
xlab="")

# Export plot to PNG
dev.copy(png, file="plot2.png")
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.
39 changes: 39 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Load the data
household_power <- read.csv("household_power_consumption.txt", sep = ";")

# Add DateTime field
household_power$DateTime <- paste(household_power$Date, household_power$Time)

# Convert Date strings to Date object
household_power$Date <- as.Date(household_power$Date, format="%d/%m/%Y")

# Convert Time strings to Time object
household_power$Time <- strptime(household_power$DateTime,
format="%d/%m/%Y %H:%M:%OS")

# Convert Sub Metering strings to numeric values
household_power$Sub_metering_1 <- as.numeric(household_power$Sub_metering_1)
household_power$Sub_metering_2 <- as.numeric(household_power$Sub_metering_2)
household_power$Sub_metering_3 <- as.numeric(household_power$Sub_metering_3)

# Filter to data from 2007-02-01 and 2007-02-02
d <- rbind(subset(household_power, household_power$Date == as.Date("2007-02-01")),
subset(household_power, household_power$Date == as.Date("2007-02-02")))

# Create the plot
plot(d$Time, d$Sub_metering_1, type="n", ylab="Energy sub metering", xlab="")

# Add the lines to the plot
lines(d$Time, d$Sub_metering_1)
lines(d$Time, d$Sub_metering_2, col="red")
lines(d$Time, d$Sub_metering_3, col="blue")

# Add the legend to the plot
legend("topright",
legend=c("Sub_metering1", "Sub_metering2", "Sub_metering3"),
col=c("black", "red", "blue"),
lty=1)

# Export plot to PNG
dev.copy(png, file="plot3.png")
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.
76 changes: 76 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Load the data
household_power <- read.csv("household_power_consumption.txt", sep = ";")

# Add DateTime field
household_power$DateTime <- paste(household_power$Date, household_power$Time)

# Convert Date strings to Date object
household_power$Date <- as.Date(household_power$Date, format="%d/%m/%Y")

# Convert Time strings to Time object
household_power$Time <- strptime(household_power$DateTime,
format="%d/%m/%Y %H:%M:%OS")

# Convert Sub Metering strings to numeric values
household_power$Sub_metering_1 <- as.numeric(household_power$Sub_metering_1)
household_power$Sub_metering_2 <- as.numeric(household_power$Sub_metering_2)
household_power$Sub_metering_3 <- as.numeric(household_power$Sub_metering_3)

# Convert Global Active Power strings to numeric values
household_power$Global_active_power <- as.numeric(household_power$Global_active_power)

# Convert Global Active Power strings to numeric values
household_power$Global_reactive_power <- as.numeric(household_power$Global_reactive_power)


# Convert Voltage strings to numeric values
household_power$Voltage <- as.numeric(household_power$Voltage)

# Filter to data from 2007-02-01 and 2007-02-02
d <- rbind(subset(household_power, household_power$Date == as.Date("2007-02-01")),
subset(household_power, household_power$Date == as.Date("2007-02-02")))

# Create the grid for the plots
par(mfrow=c(2, 2))

# Create plot #1: Global Active Power by Day
plot(d$Time,
d$Global_active_power,
type="l",
ylab="Global Active Power (kilowatts)",
xlab="")

# Create plot #2: Voltage by Day
plot(d$Time,
d$Voltage,
type="l",
ylab="Voltage",
xlab="datetime",
yaxt="none")
axis(2, seq(234, 246, 4))

# Create plot #3: Energy sub metering by Day
plot(d$Time, d$Sub_metering_1, type="n", ylab="Energy sub metering", xlab="")

# Add the lines to the plot
lines(d$Time, d$Sub_metering_1)
lines(d$Time, d$Sub_metering_2, col="red")
lines(d$Time, d$Sub_metering_3, col="blue")

# Add the legend to the plot
legend("topright",
legend=c("Sub_metering1", "Sub_metering2", "Sub_metering3"),
col=c("black", "red", "blue"),
lty=1,
bty="n")

# Create plot #4: Global Reactive Power by Daytime
plot(d$Time,
d$Global_reactive_power,
type="l",
ylab="Global_reactive_power",
xlab="datetime")

# Export plot to PNG
dev.copy(png, file="plot4.png")
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 abb6811

Please sign in to comment.