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
67 lines (56 loc) · 2.34 KB
/
Plot4.R
File metadata and controls
67 lines (56 loc) · 2.34 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
getwd()
setwd("./ExData_Plotting1")
getwd()
setwd("..")
getwd()
library(dplyr)
library(lubridate)
##Look at the first 5 rows of data
read.table("household_power_consumption.txt", sep = ";", header=TRUE, nrows = 5)
##Read in all of the data from the file in the working directory
data_all<-read.table("household_power_consumption.txt", sep = ";", header=TRUE)
data_all[data_all=="?"]<-NA
data_all[, 3:9] <- lapply(data_all[, 3:9], function(x) as.numeric(as.character(x)))
str(data_all)
head(data_all)
data_all$Time_Cont<-dmy_hms(paste(data_all$Date,data_all$Time, sep = ","))
data_all$Date<-dmy(data_all$Date)
data_all$Time<-hms(data_all$Time)
head(data_all)
##Select data for two relevant days
dat2<-filter(data_all, data_all$Date == "2007-02-01" | data_all$Date == "2007-02-02")
head(dat2)
str(dat2)
unique(dat2$Date)
dim(dat2)
##Make a graph on a screen device
par(mfrow = c(2, 2))
#graph1
with(dat2, plot(Time_Cont,Global_active_power, ylab = "Global Active Power (kilowatts)", type ="l", xlab = ""))
#graph2
with(dat2, plot(Time_Cont,Voltage, ylab = "Voltage", type ="l", xlab = "datetime"))
#graph3
with(dat2, plot(Time_Cont,Sub_metering_1, ylab = "Energy sub metering", type ="l", xlab = ""))
lines(dat2$Time_Cont,dat2$Sub_metering_2, col = "red")
lines(dat2$Time_Cont,dat2$Sub_metering_3, col = "blue")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lwd = 1, col = c("black","red","blue"))
#graph4
with(dat2, plot(Time_Cont,Global_reactive_power, ylab = "Global_reactive_power", type ="l", xlab = "datetime"))
getwd()
setwd("./ExData_Plotting1")
getwd()
##Make a png file in the repository
png(filename = "Plot4.png",width = 480, height = 480)
par(mfrow = c(2, 2))
#graph1
with(dat2, plot(Time_Cont,Global_active_power, ylab = "Global Active Power (kilowatts)", type ="l", xlab = ""))
#graph2
with(dat2, plot(Time_Cont,Voltage, ylab = "Voltage", type ="l", xlab = "datetime"))
#graph3
with(dat2, plot(Time_Cont,Sub_metering_1, ylab = "Energy sub metering", type ="l", xlab = ""))
lines(dat2$Time_Cont,dat2$Sub_metering_2, col = "red")
lines(dat2$Time_Cont,dat2$Sub_metering_3, col = "blue")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lwd = 1, col = c("black","red","blue"))
#graph4
with(dat2, plot(Time_Cont,Global_reactive_power, ylab = "Global_reactive_power", type ="l", xlab = "datetime"))
dev.off()