forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
82 lines (63 loc) · 3.28 KB
/
plot4.R
File metadata and controls
82 lines (63 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Remember to start with a new session!
#===============================================================================
# Heading
#===============================================================================
# Date: 2023-12-27
# Creator: Jesus Ortiz
# Project: exploratory_data_analysis
# Sections:
# A. Loading Data Set
# B. Transform Data for Plotting
# C. Plotting Data
#
# Notes: R-Version for this code and packages R 4.3.1
#===============================================================================
# A. Loading Data Set
#===============================================================================
household_power_consumption <- read.csv("~/Documents/01 Projects /02 Exploratory Data Analysis/01 data/household_power_consumption.txt", sep=";")
df <- household_power_consumption
#===============================================================================
# B. Transform Data for Plotting
#===============================================================================
library(dplyr)
library(lubridate)
# Create column for date and time
df$DateTime <- dmy_hms(paste(df$Date, df$Time))
# Convert date to as.Date() and filter by specified dates
df.01 <- df %>%
mutate(Date = as.Date(Date, format = "%d/%m/%Y")) %>%
filter(Date == "2007-02-01"| Date == "2007-02-02")
# Convert relevant columns to numeric
df.01 <- df.01 %>%
mutate(across(-c(Date, Time, DateTime), as.numeric))
# Create new column with name of the date
df.01$DateName <- format(df.01$Date, format = "%a")
#===============================================================================
# C. Plotting Data
#===============================================================================
#-------------------------------------------------------------------------------
# Plot 4 - Multiples graphs
#-------------------------------------------------------------------------------
# Customize x-axis labels to display abbreviated day names
unique_dates <- unique(df.01$DateName) # Get unique date names
png("plot4.png", width=480, height=480)
par(mfrow = c(2,2), mar = c(4, 4, 2, 2), oma = c(0, 0, 2, 0))
plot(df.01$DateTime, df.01$Global_active_power, type = "l", ylab = "Global Active Power", xaxt = "n", xlab = "")
axis(1, at = df.01$DateTime[c(1, 1441)], # Adjust indices for desired placement
labels = unique_dates)
plot(df.01$DateTime, df.01$Voltage, type = "l", ylab = "Voltage", xaxt = "n", xlab = "")
axis(1, at = df.01$DateTime[c(1, 1441)], # Adjust indices for desired placement
labels = unique_dates)
with(df.01, {
plot(DateTime, Sub_metering_1, type = "l", col = "black", ylab = "Energy sub metering", xaxt = "n", xlab = "")
lines(df.01$DateTime, df.01$Sub_metering_2, type = "l", col = "red")
lines(df.01$DateTime, df.01$Sub_metering_3, type = "l", col = "blue")
legend("topright", legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black", "red", "blue"), lty = 1, bty = "n") # Set bty to "n" to remove legend border
axis(1, at = df.01$DateTime[c(1, 1441)], # Adjust indices for desired placement
labels = unique_dates)
})
plot(df.01$DateTime, df.01$Global_reactive_power, type = "l", ylab = "Global_reactive_power", xaxt = "n", xlab = "")
axis(1, at = df.01$DateTime[c(1, 1441)], # Adjust indices for desired placement
labels = unique_dates)
dev.off()