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
32 lines (22 loc) · 1.1 KB
/
plot3.R
File metadata and controls
32 lines (22 loc) · 1.1 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
library(sqldf)
# Read data file, selecting only those rows between 2007-02-01 and 2007-02-02.
data <- read.csv.sql(file = 'household_power_consumption.txt', sep = ';', header = TRUE, sql = "select * from file where Date = '1/2/2007' OR Date = '2/2/2007'")
# Setup date/time format.
dateFormat <- '%d/%m/%Y %H:%M:%S'
# Combine date and time columns into single field and convert to Date object.
data$DateTime <- as.POSIXlt(paste(data$Date, data$Time), format = dateFormat)
# Set margin spacing in plot.
par(mar = c(4, 4, 2, 2))
# Set device resolution.
dev.new(width = 480, height = 480)
# Plot.
plot(data$DateTime, data$Sub_metering_1, type = 'l', xlab = '', ylab = 'Energy sub metering', cex.lab = 0.75, cex.axis = 0.8)
# Plot second line.
lines(data$DateTime, data$Sub_metering_2, col = 'Red')
# Plot third line.
lines(data$DateTime, data$Sub_metering_3, col = 'Blue')
# Draw legend.
legend('topright', legend = c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), col = c('black', 'red', 'blue'), lty = 1, lwd = 1, cex = 0.75, text.width = 45000)
# Copy to png file.
dev.copy(png, file = 'plot3.png')
dev.off()