forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.R
More file actions
44 lines (34 loc) · 1.69 KB
/
plot2.R
File metadata and controls
44 lines (34 loc) · 1.69 KB
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
###
# This script uses data from http://archive.ics.uci.edu/ml/
# The specifci data set is : https://archive.ics.uci.edu/ml/datasets/Individual+household+electric+power+consumption#
#
# This script assumes the dataset
# (https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip)
# is already downloaded and unzipped in the working directory
# (to prevent performance or networking issues)
#
# Note after unzipping the data is found in the file household_power_consumption.txt
#
# Goal : Create a graph over a 2 day period in 2007
#
# Format : PNG
# Size : 480 pixels x 480 pixels
# Read set from current working directory
# Make sure we read the date as a class Date
setClass("text2Date")
setAs("character","text2Date", function(from) as.Date(from, format="%d/%m/%Y") )
# Read all data <TODO filter while reading>
HoHoPowerCons <- read.csv("household_power_consumption.txt", header=TRUE, sep=";", stringsAsFactors = FALSE, na.strings = "?", colClasses = c("myDate", "character", rep("numeric", 7)))
# get subset for plots only interested in 2007-02-01 and 2007-02-02
# could do this in one line with the plot command...
plotperiod <- HoHoPowerCons[which(HoHoPowerCons$Date >= "2007-02-01" & HoHoPowerCons$Date <= "2007-02-02"),]
# First create empty plot with correct labels etc.
# ranges did work out
# xrange <- range(plotperiod$Date)
# yrange <- range(plotperiod$Global_active_power)
png("plot2.png")
plot(plotperiod$Global_active_power, xlab="", xaxt = "n", ylab="Global Active Power (kilowatts)", type="n")
# Add modified x axis <TODO Check if there is a better way>
axis(1, at=c(0, 1440, 2880), labels = c("Thu", "Fri", "Sat"))
lines(plotperiod$Global_active_power)
dev.off()