forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
41 lines (29 loc) · 1.37 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#This is code to generate plot 2 of project 1 from the course EDA
#This script assumes that the data file is located in your current working directory.
data1<-read.table("household_power_consumption.txt", sep=";", header=TRUE, stringsAsFactor=FALSE)
dim(data)
colnames(data)
class(data$Date)
head(data)
#subset data based on dates needed for plotting.
dataSub<-data1[as.Date(data1$Date, format = "%d/%m/%Y")=="2007-02-01" |
as.Date(data1$Date, format = "%d/%m/%Y")=="2007-02-02",]
##Check for missing/NA values coded as "?"
grep("\\?", dataSub)
#remove original dataset to free up memory
rm(data1)
#convert Date to Date/time class variables and other columns to numerics
dataSub$Date<-strptime(paste0(dataSub$Date, dataSub$Time), format = "%d/%m/%Y%H:%M:%S")
dataSub$Global_active_power<-as.numeric(dataSub$Global_active_power)
dataSub$Global_reactive_power<-as.numeric(dataSub$Global_reactive_power)
dataSub$Global_intensity<-as.numeric(dataSub$Global_intensity)
dataSub$Voltage<-as.numeric(dataSub$Voltage)
dataSub$Sub_metering_1<-as.numeric(dataSub$Sub_metering_1)
dataSub$Sub_metering_2<-as.numeric(dataSub$Sub_metering_2)
#quick peek at the data
summary(dataSub)
head(dataSub$Time)
#make desired plot
png("plot2.png", width=480, height=480)
plot(dataSub$Date, dataSub$Global_active_power, ylab="Global Activity Power (kilowatts)", xlab="", main="",type = "l")
dev.off()