forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
28 lines (22 loc) · 1.09 KB
/
plot3.R
File metadata and controls
28 lines (22 loc) · 1.09 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
# Read data
set <- read.table("household_power_consumption.txt", header = TRUE, sep = ";", colClasses = "character")
# Get column names
cols <- colnames(set)
# Create "DateTime" column with combination of date and time
set$DateTime <- strptime(paste(set$Date, set$Time), format = "%d/%m/%Y %H:%M:%S", tz = "UTC")
# Set data types
set$Date <- as.Date(set$Date, "%d/%m/%Y")
set$Global_active_power <- as.numeric(set$Global_active_power)
# Subset data set to the 2 relevant dates
set <- subset(set, set$Date %in% as.Date(c("2007-02-01", "2007-02-02")))
# Create plot with first variable (Sub_metering_1)
with(set, plot(DateTime, Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering"))
# Add second variable to plot (Sub_metering_2)
with(set, lines(DateTime, Sub_metering_2, col = "red"))
# Add third variable to plot (Sub_metering_2)
with(set, lines(DateTime, Sub_metering_3, col = "blue"))
# Add legend to plot
legend("topright",legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, lwd=1.5,col=c("black","blue","red"))
# Save as png
dev.copy(png, file = "plot3.png")
dev.off()