forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
26 lines (23 loc) · 1.31 KB
/
plot2.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
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)
## 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 plot in the screen device
plot(working_data$DateTime,working_data[,3],type="l",xlab=" ",ylab="Global Active Power (kilowatts)")
######Copy plot to png file
dev.copy(png, file="plot2.png",width=480, height=480)
dev.off()