-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
51 lines (41 loc) · 1.88 KB
/
plot2.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
48
49
50
## Peer-Graded Assignment: 'Exploratory Data Analysis' Course Project #1
## part of coursera's nine-week Data Spezialization by Johns Hopkins University
## February 06, 2018
## author: Thomas Fischer
## email: tomfischer@qq.com
# Part 02/04 plotting plot02.png
# ----------- Getting Data -----------------------------------------------------
setwd('C:/Users/user/Documents/GitHub/ExploratoryDataAnalysisProject01/')
rm(list = ls())
dir('./data')
# Load data in data.table object
require(data.table)
power_consumption <- fread(file = 'data/household_power_consumption.txt',
na.strings = '?'
)
# The dataset has 2,075,259 rows and 9 columns.
dim(power_consumption)
head(power_consumption)
# We will only be using data from the dates 2007-02-01 and 2007-02-02.
power_consumption <- power_consumption[Date == '1/2/2007' | Date == '2/2/2007']
power_consumption[, datetime :=
as.POSIXct(paste(Date, Time),
"%d/%m/%Y %H:%M:%S",
tz = '')]
sapply(power_consumption, range)
# ----------- Plotting on screen -----------------------------------------------
with(power_consumption, plot(x = datetime,
y = Global_active_power,
type = 'l',
xlab = '',
ylab = 'Global Active Power (kilowatts)'))
# ----------- Plotting to file -------------------------------------------------
# Construct the plot and save it to a PNG file with a width of 480 pixels
# and a height of 480 pixels.
png(file = 'plot2.png', height = 480, width = 480)
with(power_consumption, plot(x = datetime,
y = Global_active_power,
type = 'l',
xlab = '',
ylab = 'Global Active Power (kilowatts)'))
dev.off()