forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
30 lines (25 loc) · 1.33 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
###Code to use for subsetting data for plot generation
###Assumes data file is in current working directory
#read some rows to determine class type for each column
first5rows <- read.table("household_power_consumption.txt", header = TRUE, sep = ";", nrows = 5, na.strings = "?")
classes <- sapply(first5rows, class)
#read in all the data using the identified classes
alldata <- read.table("household_power_consumption.txt", header = TRUE, colClasses = classes, sep = ";", nrows = 210000, na.strings = "?")
#convert Date column to Date data class
alldata$Date <- as.Date(alldata$Date, format = '%d/%m/%Y')
min = as.Date("2007-01-31")
max = as.Date("2007-02-03")
#extract the subset of data we want to work with
data <- subset(alldata, Date > min & Date < max)
#remove the larger data set to conserve memory
rm(alldata)
#merge data and time columns to form datetime information
data$datetime <- paste(as.character(data$Date), data$Time, sep = ' ')
#convert datetime from char to date class
data$datetime <- strptime(data$datetime, format='%Y-%m-%d %H:%M:%S')
png(file = "plot2.png", width = 480, height = 480)
#plot Global_active_power with very small points
with(data, plot(datetime, Global_active_power, pch = '.', ylab ="Global Active Power (kilowatts)", xlab = "" ))
#overlay plot with black line
with(data, lines(datetime, Global_active_power))
dev.off()