forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
53 lines (41 loc) · 2.54 KB
/
plot4.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
49
50
51
52
53
## File: plot4.R, a function for the 1st Course Project of the Exploratory analysis course
## Web: https://class.coursera.org/exdata-004/
## Authors: rdpeng, gabrielfc
## Date: 2014/07
## Description: This R script has 1 function: Reconstruct a given plot ('plot4.png') in the course.
## Important: The axes on the plot are on Spanish because that's my OS language.
## Set the Working directory on the root of the repo.
setwd("~/Random/Exploratory Analysis Course/Project 1")
## Import the downloaded file into R.
power_consumption_data <- read.csv("household_power_consumption.txt", sep=";", na.strings="?")
## Subset on the 2 days we are working
working_data <- subset(power_consumption_data, (power_consumption_data$Date == "1/2/2007" | power_consumption_data$Date== "2/2/2007"))
## For this plot to work, R has to understand that the strings are Dates and Hours.
## We start creating a new column with the combination of the strings.
working_data$FormattedTime <- paste(working_data$Date,working_data$Time)
## And now we specify the input format to strptime to convert them.
## Time is already well formatted, but we have to modify the formatting of the Dates.
working_data$FormattedTime <- strptime(working_data$FormattedTime, "%d/%m/%Y %H:%M:%S")
## Recreate the plot
## Iniatialize png export
png(filename="plot4.png", width=480, height=480)
## So in this plot, we have four different plots, which we can join with the par function and mfcol / mfrow options.
## Using mfrow they will be plot in row order.
par(mfrow = c(2,2))
## First plot (plot2.png)
plot(working_data$FormattedTime, working_data$Global_active_power, type = "l", xlab="", ylab = "Global Active Power")
## Second plot
plot(working_data$FormattedTime, working_data$Voltage, type="l", xlab="datetime", ylab="Voltage")
## Third plot (plot3.png)
## We have to plot 3 different lines, Sub_metering_1, 2 and 3.
## Start with the first in black.
plot(working_data$FormattedTime, working_data$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering")
## Add the second in red and the third in blue.
lines(working_data$FormattedTime, working_data$Sub_metering_2, type = "l", col = "red")
lines(working_data$FormattedTime, working_data$Sub_metering_3, type = "l", col = "blue")
## Finally, the legend on the top right.
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty = 1, col = c("black", "red", "blue"))
## Fourth plot
plot(working_data$FormattedTime, working_data$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power")
## Finish the export
dev.off()