forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
48 lines (39 loc) · 1.6 KB
/
plot3.R
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
#Read header and only read a subset of the data
household_name <- read.csv2(file = "household_power_consumption.txt",
na.strings = "?",
comment.char = "",
stringsAsFactors = FALSE,
nrows = 3)
household <- read.csv2(file = "household_power_consumption.txt",
na.strings = "?",
comment.char = "",
stringsAsFactors = FALSE,
skip = 66637,
nrows = 2879)
names(household) <- names(household_name)
rm(household_name)
#Type convert
household$DateTime <- as.POSIXct(paste(as.Date(household$Date, "%d/%m/%Y"),
household$Time,
sep = " ")
)
# Set LC_TIME to en_US to show proper weekday
Sys.setlocale(category = "LC_TIME", locale = "en_US.UTF-8")
#Initialize png device as dev.copy seems to have problem
png(filename = 'plot3.png', width = 480, height = 480, units = 'px')
#Plot lines with legend
plot(x = household$DateTime,
y = household$Sub_metering_1,
col = "black",
type = "l",
ylab = "Energy sub metering",
xlab = "")
lines(x = household$DateTime, y = household$Sub_metering_2, col="red")
lines(x = household$DateTime, y = household$Sub_metering_3, col="blue")
legend("topright", lty = 1, col = c("black", "red", "blue"),
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
)
#Close device to conclude the png
dev.off()
#Some cleaning
rm(household)