forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
25 lines (20 loc) · 1.08 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
23
24
## Electric power consumption: histogram of measurements of Global Active Power
# (2007-02-01 and 2007-02-02)
# Extracted csv file (put to "../data" directory), data source:
# https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
# Reading dataset form csv file (if it doesn't exists allready)
if(!exists("df.all")) {
df.all <- read.csv("../data//household_power_consumption.txt", sep = ";", na.strings = "?")
} else {
print("skipping read.csv, using existing data...")
}
# subseting only data for required dates
df <- subset(df.all, df.all$Date == '1/2/2007' | df.all$Date == '2/2/2007')
# Transforming strings in Date column to dates and appending DateTime column
df$Date <- as.Date(df$Date, format = "%d/%m/%Y")
df$DateTime <- strptime(paste(df$Date, " ", df$Time), "%Y-%m-%d %H:%M:%S")
# plotting histogram to png image file - transparent background as in examples
png("plot1.png", width = 480, height = 480, bg = "transparent")
hist(df$Global_active_power, main = "Global Active Power",
xlab = "Global Active Power (kilowatts)", col = "red")
dev.off()