forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
48 lines (43 loc) · 2.29 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
library(lubridate)
##Get the downloaded data from txt file as table ()
## https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
dwlded_data <- read.table("./household_power_consumption.txt", sep=";",
header=TRUE, dec=".", na.strings = "?")
##Convert the Date and Time variables to Date/Time classes in R
##using the strptime() and as.Date() functions.
dwlded_data$Date<-dmy(dwlded_data$Date)
dwlded_data$Date<-suppressWarnings(as.Date(dwlded_data$Date,"%d/%m/%Y"))
##Create unified DateTime variable
dwlded_data$DateTime <- strptime(paste(dwlded_data$Date,dwlded_data$Time),"%Y-%m-%d %H:%M:%S")
## Convert measures as numeric
dwlded_data$Global_active_power<-as.numeric(dwlded_data$Global_active_power
,na.rm = TRUE)
dwlded_data$Global_reactive_power<-as.numeric(dwlded_data$Global_reactive_power
,na.rm = TRUE)
dwlded_data$Sub_metering_1<-as.numeric(dwlded_data$Sub_metering_1
,na.rm = TRUE)
dwlded_data$Sub_metering_2<-as.numeric(dwlded_data$Sub_metering_2
,na.rm = TRUE)
dwlded_data$Sub_metering_3<-as.numeric(dwlded_data$Sub_metering_3
,na.rm = TRUE)
## Set the working data, subsetting data by Date
working_data<-subset(dwlded_data,dwlded_data$Date %in% as.Date(c("2007-02-01","2007-02-02")))
######Get plots in the screen device
## Define distribution of plots in screen
par(mfrow=c(2,2))
##plot1
plot(working_data$DateTime,working_data[,3],type="l",xlab=" ",
ylab="Global Active Power", cex=0.2)
##plot2
plot(working_data$DateTime,working_data[,5],type="l",xlab="datetime",ylab="Voltage")
##plot3
plot(working_data$DateTime,working_data[,7],type="l",xlab=" ",ylab="Energy sub metering")
lines(working_data[,10],working_data[,8],col="Red")
lines(working_data[,10],working_data[,9],col="Blue")
legend(x="topright",legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3")
,col=c("Black","Red","Blue"),lwd=1,pt.cex=0.3,cex=0.3,bty="n")
##plot4
plot(working_data$DateTime,working_data[,4],type="l",xlab="datetime",ylab="Global_reactive_power")
######Copy plot to png file
dev.copy(png, file="plot4.png",width=480, height=480)
dev.off()