forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
22 lines (19 loc) · 1.05 KB
/
plot1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
###Code to use for subsetting data for plot generation
#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)
#open png graphics device and set desired width and height of output
png(file = "plot1.png", width = 480, height = 480)
#plot histogram with desired color and labels
with(data, hist(data$Global_active_power, col = 'red', xlab = "Global Active Power (kilowatts)", main = "Global Active Power"))
dev.off()