forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
28 lines (22 loc) · 1.13 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
25
26
27
28
plot1 <- function() {
# Installing and loading the required packages for this script
if(system.file(package = "dplyr") == "") install.packages("dplyr", quiet = TRUE)
library(dplyr, quietly = TRUE)
# Reading the power consumption file
fileName <- "household_power_consumption.txt"
tmp <- read.table(fileName, header = TRUE, sep = ";", na.strings = "?")
# Subsetting to only include dates 2007-02-01 and 2007-02-02
dat <- subset(tmp, Date == "1/2/2007"| Date == "2/2/2007")
# Converting the separate date and time columns into a single column
# represented as a POSIXct class
dat <- dat %>%
mutate(datetime = paste0(Date, " ", Time)) %>%
mutate(datetime = as.POSIXct(datetime, format = "%d/%m/%Y %H:%M:%S"))
# Opening the png device
png(file = "plot1.png", width = 480, height = 480, units = "px")
# Constructing the plot in the current device
with(dat, hist(Global_active_power, col = "red", main = "Global Active Power",
xlab = "Global Active Power (kilowatts)", ylab = "Frequency"))
# Closing the current device
dev.off()
}