Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create R_Script #1025

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions R_Script
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Load necessary libraries
library(data.table)

# Read data
data <- fread("household_power_consumption.txt", na.strings = "?", sep = ";")

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

# Filter data for February 1 and 2, 2007
filtered_data <- subset(data, Date == "2007-02-01" | Date == "2007-02-02")

# Combine Date and Time into a single datetime column
filtered_data$Datetime <- as.POSIXct(paste(filtered_data$Date, filtered_data$Time), format="%Y-%m-%d %H:%M:%S")

# Open PNG device
png("plot1.png", width = 480, height = 480)

# Create the histogram
hist(filtered_data$Global_active_power, col="red", main="Global Active Power",
xlab="Global Active Power (kilowatts)", ylab="Frequency")

# Close the device
dev.off()

# Open PNG device
png("plot2.png", width = 480, height = 480)

# Create the time series plot
plot(filtered_data$Datetime, filtered_data$Global_active_power, type="l",
xlab="", ylab="Global Active Power (kilowatts)")

# Close the device
dev.off()

# Open PNG device
png("plot3.png", width = 480, height = 480)

# Create the plot
plot(filtered_data$Datetime, filtered_data$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
lines(filtered_data$Datetime, filtered_data$Sub_metering_2, col="red")
lines(filtered_data$Datetime, filtered_data$Sub_metering_3, col="blue")

# Add legend
legend("topright", col=c("black", "red", "blue"), lty=1,
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))

# Close the device
dev.off()

# Open PNG device
png("plot4.png", width = 480, height = 480)

# Set up a 2x2 plotting layout
par(mfrow=c(2,2))

# Plot 1: Global Active Power
plot(filtered_data$Datetime, filtered_data$Global_active_power, type="l",
xlab="", ylab="Global Active Power")

# Plot 2: Voltage
plot(filtered_data$Datetime, filtered_data$Voltage, type="l",
xlab="datetime", ylab="Voltage")

# Plot 3: Energy Sub Metering
plot(filtered_data$Datetime, filtered_data$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
lines(filtered_data$Datetime, filtered_data$Sub_metering_2, col="red")
lines(filtered_data$Datetime, filtered_data$Sub_metering_3, col="blue")
legend("topright", col=c("black", "red", "blue"), lty=1, bty="n",
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))

# Plot 4: Global Reactive Power
plot(filtered_data$Datetime, filtered_data$Global_reactive_power, type="l",
xlab="datetime", ylab="Global Reactive Power")

# Close the device
dev.off()