Skip to content

Commit d98bc91

Browse files
committed
last plot
1 parent b6f8a86 commit d98bc91

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

plot4.R

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# setwd("E:/Courses/Coursera/DSS4 - EDA/projects/prj1")
2+
3+
# set locale in order to get English names for time related variables
4+
Sys.setlocale("LC_TIME", "English")
5+
6+
# define the columns dataype
7+
cols <- c(rep("character", 2), rep("numeric", 7))
8+
9+
# compute skip and nrows parameters for read.table function
10+
d0 <- as.POSIXct("16/12/2006 17:24:00", format="%d/%m/%Y %H:%M:%S")
11+
d1 <- as.POSIXct("1/2/2007", format="%d/%m/%Y")
12+
d2 <- as.POSIXct("3/2/2007", format="%d/%m/%Y")
13+
skip <- as.numeric(difftime(d1, d0, units="mins")) + 1 # header row
14+
nrows <- as.numeric(difftime(d2, d1, units="mins"))
15+
16+
# read only the required part fo the data set
17+
hepc <- read.table("household_power_consumption.txt", sep=";", colClasses=cols,
18+
nrows=nrows, skip=skip, stringsAsFactors=FALSE,
19+
na.strings="?", comment.char="")
20+
21+
# set the name from the first row
22+
names(hepc) <- read.table("household_power_consumption.txt", sep=";", nrows=1,
23+
stringsAsFactors=FALSE)
24+
25+
# create the datetime variable
26+
hepc$datetime <- as.POSIXct(paste(hepc$Date, hepc$Time, sep=" "),
27+
format="%d/%m/%Y %H:%M:%S")
28+
29+
# add 'day of week' variable
30+
hepc$dow = as.factor(weekdays(hepc$datetime, abbreviate=TRUE)) # day of week
31+
32+
# keep only relevantvariables and reorder them
33+
hepc <- hepc[c(10:11, 3:9)]
34+
35+
# Plot4
36+
png(file="plot4.png", width=480, height=480, bg="transparent")
37+
38+
par(mfrow=c(2,2))
39+
40+
with(hepc, {
41+
plot(datetime, Global_active_power, type = "l", xlab = "",
42+
ylab = "Global Active Power", main = "")
43+
44+
plot(datetime, Voltage, type = "l", main = "")
45+
46+
plot(hepc$datetime, hepc$Sub_metering_1, type = "n", xlab = "",
47+
ylab = "Energy sub metering", main = "")
48+
lines(hepc$datetime, hepc$Sub_metering_1, col = "black")
49+
lines(hepc$datetime, hepc$Sub_metering_2, col = "red")
50+
lines(hepc$datetime, hepc$Sub_metering_3, col = "blue")
51+
legend("topright", names(hepc)[7:9], col=c('black','red', 'blue'), bty="n")
52+
53+
plot(datetime, Global_reactive_power, type = "l", main = "")
54+
})
55+
56+
dev.off()
57+
58+
# reset time locale to the default value
59+
Sys.setlocale("LC_TIME", "")

plot4.png

7.02 KB
Loading

0 commit comments

Comments
 (0)