forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
96 lines (94 loc) · 5.99 KB
/
plot4.R
File metadata and controls
96 lines (94 loc) · 5.99 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Course Project 1 in Coursera Exploratory Data Analysis
## The file file creates the first plot in the assignment.
## The function uses data that has been downloaded from here
## URL="https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## The file household_power_consumption.txt has been extracted from the zip file and placed in data subfolder
## The file household_power_consumption.txt is input and the result of the processing is a histogram saved as PNG file plot4.png
plot4 <- function() {
library(dplyr)
library(timeDate)
## Name data variables that will be used
df_power_con <- data.frame()
## Read data into data frame
df_power_con <- read.table("data/household_power_consumption.txt", header= TRUE, sep = ";", na.strings = "?", stringsAsFactors = FALSE)
## Transform the Date to a true date field so it is easier to select from
df_power_con$Date <- as.Date(df_power_con$Date, "%d/%m/%Y")
## Create a subset that only has observartion from the two required dates
subset_df_power_con = subset(df_power_con, Date >= '2007-02-01' & Date <= '2007-02-02')
## Create a new column that has a combined date and time field
dates <- subset_df_power_con$Date
times <- subset_df_power_con$Time
date_time <- paste(dates, times)
strptime(date_time, "%Y/%m/d %H:%M:%S")
subset_df_power_con <- cbind(date_time, subset_df_power_con)
row_count <- NROW(subset_df_power_con$Date)
last_date <- as.Date(as.Date(subset_df_power_con$date_time[row_count]) + 1)
first_date <- as.Date(subset_df_power_con$date_time[1])
## first_day <- dayOfWeek(as.timeDate(subset_df_power_con$date_time[1]))
day_count <- as.numeric(last_date - first_date + 1)
## middle_date <- as.Date(first_date + (day_count / 2))
day <- vector()
position <- 0
tick_pos <- vector()
## We set the positions of the first day on the x axis
tick_pos[1] <- 1
for (i in 1:day_count) {
day[i] <- dayOfWeek(as.timeDate(first_date + i - 1)) ## we need to subtract one to get the right date
if (i > 1 & i < day_count) {
## we calculate the x axis tick positions between the first and the last by finding the row number of the date
## the day we look at is the previous day, so that we get first position of current day
tick_pos[i] <- tick_pos[i - 1] + length(which(subset_df_power_con$Date == as.Date(first_date + i - 2) , arr.ind=TRUE))
}
}
## We set the positions of the last day on the x axis
tick_pos[day_count] <- row_count
## Show the plot on the screen, just to make it quick and easy to see the result. Save to png done after the screen part.
par(mfrow = c(2, 2), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
## Show plot 1
plot(subset_df_power_con$date_time, subset_df_power_con$Global_active_power, type="n", xaxt="n", ylab = "Global Active Power (kilowatts)", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Global_active_power)
axis(1, at = tick_pos, labels = day, tick = TRUE)
## Show plot 2
plot(subset_df_power_con$date_time, subset_df_power_con$Voltage, type="n", xaxt="n", ylab = "Voltage", xlab = "datetime", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Voltage)
axis(1, at = tick_pos, labels = day, tick = TRUE)
## Show plot 3
plot(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_1, type="n", xaxt="n", ylab = "Energy sub metering", col = "black", border = "black")
axis(1, at = tick_pos, labels = day, tick = TRUE)
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_1, col="black")
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_2, col="red")
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_3, col="blue")
legend("topright",
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
bty = "n",lwd=2, cex=1.05,y.intersp=1.2, col=c("black","red","blue"), lty=c(1,1,1))
## Show plot 4
plot(subset_df_power_con$date_time, subset_df_power_con$Global_reactive_power, type="n", xaxt="n", ylab = "Global_reactive_power", xlab = "datetime", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Global_reactive_power)
axis(1, at = tick_pos, labels = day, tick = TRUE)
## Save the plot as png file in working dir
png(file = "plot4.png", bg = "transparent")
par(mfrow = c(2, 2), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
## Show plot 1
plot(subset_df_power_con$date_time, subset_df_power_con$Global_active_power, type="n", xaxt="n", ylab = "Global Active Power (kilowatts)", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Global_active_power)
axis(1, at = tick_pos, labels = day, tick = TRUE)
## Show plot 2
plot(subset_df_power_con$date_time, subset_df_power_con$Voltage, type="n", xaxt="n", ylab = "Voltage", xlab = "datetime", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Voltage)
axis(1, at = tick_pos, labels = day, tick = TRUE)
## Show plot 3
plot(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_1, type="n", xaxt="n", ylab = "Energy sub metering", col = "black", border = "black")
axis(1, at = tick_pos, labels = day, tick = TRUE)
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_1, col="black")
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_2, col="red")
lines(subset_df_power_con$date_time, subset_df_power_con$Sub_metering_3, col="blue")
legend("topright",
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
bty = "n",lwd=2, cex=1.05,y.intersp=1.2, col=c("black","red","blue"), lty=c(1,1,1))
## Show plot 4
plot(subset_df_power_con$date_time, subset_df_power_con$Global_reactive_power, type="n", xaxt="n", ylab = "Global_reactive_power", xlab = "datetime", col = "black", border = "black")
lines(subset_df_power_con$date_time, subset_df_power_con$Global_reactive_power)
axis(1, at = tick_pos, labels = day, tick = TRUE)
dev.off()
## browser()
}