forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.r
105 lines (94 loc) · 2.81 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
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
101
102
103
104
105
## Set the path to the data directory
print(dir.data <- paste0(getwd(),"/"))
## Make a variable to hold the path to the file we want to import
print(path.abs.data.src <- paste0(
dir.data
,"household_power_consumption.txt"
))
## Confirm we have downloaded the data properly
stopifnot(file.exists(path.abs.data.src))
## Import the data to R
head(power.consumption <- read.table(
path.abs.data.src
,sep = ";"
,header = TRUE
,na.string = "?"
))
## Briefly explore to see how the data imported
str(power.consumption)
summary(power.consumption)
## Convert the dates to actual dates
power.consumption$DateTime <- strptime(paste(power.consumption$Date,power.consumption$Time), format = "%d/%m/%Y %H:%M:%S")
power.consumption$DateAsDate <- as.Date(power.consumption$Date, format = "%d/%m/%Y")
## Check the structure post conversion
str(power.consumption)
## Limit to the data we want to plot
power.consumption.limit <- subset(power.consumption
,power.consumption$DateAsDate == as.Date("2007-02-01")
| power.consumption$DateAsDate == as.Date("2007-02-02")
)
## Confirm our limitations worked
print(unique(power.consumption.limit$DateAsDate))
## See the list of named (red) colors available. Useful for development
# print(grep("^(red|black|blue)"
# ,colors()
# ,ignore.case = TRUE
# , value = TRUE
# )
# )
## Make the actual plot
png("plot4.png"
,width = 480
,height = 480
,units = "px"
)
par(mfrow = c(2,2))
plot(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Voltage
,type = "l" ## type == line. Lines instead of points
,xlab = "datetime"
,ylab = "Voltage"
)
plot(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Global_active_power
,type = "l" ## type == line. Lines instead of points
,xlab = ""
,ylab = "Global Active Power (kilowatts)"
)
plot(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Sub_metering_1
,type = "l" ## type = line (lines instead of points)
,lty = 1
,xlab = ""
,ylab = "Energy sub metering"
,col = "black"
)
lines(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Sub_metering_2
,lty = 1
,col = "red"
)
lines(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Sub_metering_3
,lty = 1
,col = "blue"
)
legend("topright"
,c("Sub_metering_1"
,"Sub_metering_2"
,"Sub_metering_3"
)
,lty = 1
,col = c("black"
,"red"
,"blue"
)
,bty = "n" ## Do not show a box around the legend
)
plot(x = power.consumption.limit$DateTime
,y = power.consumption.limit$Global_reactive_power
,type = "l" ## type = line (lines instead of points)
,xlab = "datetime"
,ylab = "Global_reactive_power"
)
dev.off()