Skip to content

Commit b6f8a86

Browse files
committed
first three plots
1 parent 73fe5c6 commit b6f8a86

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed

plot1.R

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# Plot1
36+
png(file="plot1.png", width=480, height=480, bg="transparent")
37+
38+
hist(hepc$Global_active_power, xlab = "Global Active Power (kilowatts)",
39+
breaks=16, main="Global Active Power", col="red")
40+
41+
dev.off()
42+
43+
# reset time locale to the default value
44+
Sys.setlocale("LC_TIME", "")

plot1.png

3.6 KB
Loading

plot2.R

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# Plot2
36+
png(file="plot2.png", width=480, height=480, bg="transparent")
37+
38+
plot(hepc$datetime, hepc$Global_active_power, type = "l", xlab = "",
39+
ylab = "Global Active Power (kilowatts)", main = "")
40+
41+
dev.off()
42+
43+
# reset time locale to the default value
44+
Sys.setlocale("LC_TIME", "")

plot2.png

4.44 KB
Loading

plot3.R

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# Plot3
36+
png(file="plot3.png", width=480, height=480, bg="transparent")
37+
38+
plot(hepc$datetime, hepc$Sub_metering_1, type = "n", xlab = "",
39+
ylab = "Energy sub metering", main = "")
40+
lines(hepc$datetime, hepc$Sub_metering_1, col = "black")
41+
lines(hepc$datetime, hepc$Sub_metering_2, col = "red")
42+
lines(hepc$datetime, hepc$Sub_metering_3, col = "blue")
43+
legend("topright", names(hepc)[7:9], col=c('black','red', 'blue'), lty=1)
44+
45+
dev.off()
46+
47+
# reset time locale to the default value
48+
Sys.setlocale("LC_TIME", "")

plot3.png

3.89 KB
Loading

0 commit comments

Comments
 (0)