forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
47 lines (38 loc) · 1.43 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##
# Exploratory Data Aanalysis / Johns Hopkins / Coursera
# Project 1
# June 2015
##
library(tidyr)
library(dplyr)
## Loading the dataset
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
datafn <- "household_power_consumption.txt"
pngfn <- "plot1.png"
if (!file.exists(datafn))
{
tmp <- tempfile()
download.file(url, tmp)
datafn <- unz(tmp, datafn)
}
data <- read.csv(datafn, sep=";", stringsAsFactors=FALSE)
data <- tbl_df(data)
## Tidying Date & Time
data <- unite(data, "DateTime", Date, Time, sep=" ")
data$DateTime <- as.POSIXct(strptime(data$DateTime, "%d/%m/%Y %T"))
## Filtering by date
data <- filter(data, DateTime >= "2007-02-01" & DateTime < "2007-02-03")
## Tidying numeric columns
data$Global_active_power <- as.numeric(data$Global_active_power)
data$Global_reactive_power <- as.numeric(data$Global_reactive_power)
data$Voltage <- as.numeric(data$Voltage)
data$Global_intensity <- as.numeric(data$Global_intensity)
data$Sub_metering_1 <- as.numeric(data$Sub_metering_1)
data$Sub_metering_2 <- as.numeric(data$Sub_metering_2)
data$Sub_metering_3 <- as.numeric(data$Sub_metering_3)
# Plotting to screen
hist(data$Global_active_power, col="red", xlab="Global Active Power (kilowatts)", main="Global Active Power")
## Plotting to PNG
png(file=pngfn, bg="transparent")
hist(data$Global_active_power, col="red", xlab="Global Active Power (kilowatts)", main="Global Active Power")
dev.off()