forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
98 lines (67 loc) · 2.95 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
#Add code here
#install.packages("data.table")
library(data.table)
datafile <- "household_power_consumption.txt"
# Get the Training Data
pol_data <- read.table(datafile,sep=";",header = TRUE)
#install.packages("dplyr")
library(dplyr)#Add code here
#Convert to POSIX Date and format,
pol_data$Date <- as.Date(strptime(pol_data$Date,"%d/%m/%Y"),"%Y-%m-%d" )
#Format text to be coerced to numerics later
#pol_data$Global_active_power <- as.numeric(gsub(",","",pol_data$Global_active_power))
#pol_data$Global_active_power <- as.numeric(pol_data$Global_active_power)
smalldata <- subset(pol_data, Date >= "2007-02-01" & Date <= "2007-02-02")
smalldata$Global_active_power <- as.numeric(gsub(",","",smalldata$Global_active_power))
smalldata$Global_active_power <- as.numeric(smalldata$Global_active_power)
#smalldata$Sub_metering_1 <- as.numeric(smalldata$Sub_metering_1)
#smalldata$Sub_metering_2 <- as.numeric(smalldata$Sub_metering_2)
#smalldata$Sub_metering_3 <- as.numeric(smalldata$Sub_metering_3)
#need to get the day from date, or replace date with Week day, or add a column
#install.packages("lubridate")
library(lubridate)
#wday(smalldata$Date,label = TRUE)
newds <- mutate(smalldata, day = wday(smalldata$Date,label = TRUE))
#Convert Sub Metering to numeric
newds$Sub_metering_1 <- as.numeric(gsub(",","",newds$Sub_metering_1))
newds$Sub_metering_1 <- as.numeric(newds$Sub_metering_1)
newds$Sub_metering_2 <- as.numeric(gsub(",","",newds$Sub_metering_2))
newds$Sub_metering_2 <- as.numeric(newds$Sub_metering_2)
newds$Voltage <- as.numeric(gsub(",","",newds$Voltage))
newds$Voltage <- as.numeric(newds$Voltage)
newds$Global_reactive_power <- as.numeric(gsub(",","",newds$Global_reactive_power))
newds$Global_reactive_power <- as.numeric(newds$Global_reactive_power)
dates <- smalldata$Date
times <- smalldata$Time
x <- paste(dates, times)
newds$Datetime <- strptime(x, "%Y-%m-%d %H:%M:%S")
#Plotting
par(mfrow=c(2,2))
#Graph 1
xrange <- newds$Datetime
yrange <- newds$Global_active_power
plot(xrange,yrange, typ='l',ylab = "Global Active Power(kilowatts)",xlab = "")
#Graph 3
xrange <- newds$Datetime
yrange <- newds$Voltage
plot(xrange,yrange, typ='l',ylab = "Voltage",xlab = "datetime")
#Graph 2
xrange <- newds$Datetime
yrange <- newds$Sub_metering_1
yrange2 <- newds$Sub_metering_2
yrange3 <- newds$Sub_metering_3
plot(xrange,yrange, typ='l',ylab = "Energy sub metering",xlab = "")
#Multiple series
points(xrange, yrange2, col='red', type='l', lwd=2)
points(xrange, yrange3, col='blue', type='l', lwd=2)
#Now we add the Legend
legend("topright",
legend=c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'),
col=c('black', 'red', 'blue'), lwd=3, lty=c(1,1,2))
#Graph 4
xrange <- newds$Datetime
yrange <- newds$Global_reactive_power
plot(xrange,yrange, typ='l',ylab = "Global_reactive_power",xlab = "datetime")
######Note copying to device, breaks rendering on the Legend
dev.copy(device = png, filename = 'plot4.png', width = 480, height = 480)
dev.off()