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
100 lines (83 loc) · 3.79 KB
/
Copy pathplot4.R
File metadata and controls
100 lines (83 loc) · 3.79 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
97
98
99
100
## Data can be found at this link, was originally accessed on August 10, 2014
## https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
## File was then unzipped on the desktop
## Read data into R
data <- read.table("/Users/Mario/Desktop/household_power_consumption.txt",sep=";",header=TRUE)
## Give the columns their appropriate names
names(data) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3")
## Create new column, DateTime, with formatted date and time
data$DateTime <- paste(data$Date,data$Time)
data$DateTime <- strptime(data$DateTime, format="%d/%m/%Y %T")
## Subset the dates we want (this could have been done sooner to save time)
data2 <- subset(data, (as.Date(data$DateTime)=="2007-02-01" | as.Date(data$DateTime)=="2007-02-02"))
## Correct some data types (for some reason, just using as.numeric led to some errors)
## We will not be using columns 1 and 2 ("Date" and "Time") again so those can be left alone
data2$Global_active_power <- as.numeric(as.character(data2$Global_active_power))
data2$Global_reactive_power <- as.numeric(as.character(data2$Global_reactive_power))
data2$Voltage <- as.numeric(as.character(data2$Voltage))
data2$Global_intensity <- as.numeric(as.character(data2$Global_intensity))
data2$Sub_metering_1 <- as.numeric(as.character(data2$Sub_metering_1))
data2$Sub_metering_2 <- as.numeric(as.character(data2$Sub_metering_2))
data2$Sub_metering_3 <- as.numeric(as.character(data2$Sub_metering_3))
### --------------------------------------------------------------------------###
### Now we make the FOURTH plot and save it
### --------------------------------------------------------------------------###
png("/Users/Mario/ExData_Plotting1/plot4.png", width=480, height=480)
par(mfrow=c(2,2))
## Plot A
## ---------------------------------------------------------------------------------
plot(x=data2$DateTime,
y=data2$Global_active_power,
type="l",
ylab="Global Active Power",
xlab="",
lwd=1.25, ## Make a little thicker
cex.lab=1)
## ---------------------------------------------------------------------------------
## Plot B
## ---------------------------------------------------------------------------------
plot(x=data2$DateTime,
y=data2$Voltage,
xlab="datetime",
ylab="Voltage",
type="l",
lwd=1.25) ## make lines a tad thicker
## ---------------------------------------------------------------------------------
## Plot C
## ---------------------------------------------------------------------------------
## Add the first set of points, Sub_metering_1, and put in the x and y axis labels
plot(x=data2$DateTime,
y=data2$Sub_metering_1,
type="l",
col="black",
xlab="",
ylab="Energy sub metering")
## Add in the second set of points, Sub_metering_2
points(x=data2$DateTime,
y=data2$Sub_metering_2,
type="l",
col="red")
## Add in the third set of points, Sub_metering_3
points(x=data2$DateTime,
y=data2$Sub_metering_3,
type="l",
col="blue")
## Add legend
legend("topright",
col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
lwd=c(2,2,2), ## make it a little darker so it looks better
bty="n", ## Remove that border!!!
cex=1)
## ---------------------------------------------------------------------------------
## Plot D
## ---------------------------------------------------------------------------------
plot(x=data2$DateTime,
y=data2$Global_reactive_power,
xlab="datetime",
ylab="Global_reactive_power",
type="l",
lwd=1.2, ## Make lines a little thicker
cex=.1)
## ---------------------------------------------------------------------------------
dev.off()