forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
34 lines (25 loc) · 1.47 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
########################################################
# Assignment course 4, Week 1, plot 2
########################################################
# Downloading data:
if (!file.exists("raw_data.zip")){
fileURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(fileURL, "raw_data.zip", method="curl")
}
if (!file.exists("household_power_consumption.txt")) {
unzip("raw_data.zip")
}
# extract data o
f February first and second
names <- c("Date", "Time", "Global_active_power", "Global_reactive_power", "Voltage", "Global_intensity", "Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data <- read.table( "household_power_consumption.txt", nrows=2881, skip=66637, sep=";", col.names = names )
# Creating a new column of the full date and converting its type
data$fullDate <- apply(select(data, c("Date", "Time")), MARGIN = 1, function(x){paste(x[1], x[2])})
data$fullDate <- strptime(data$fullDate, format = "%d/%m/%Y %H:%M:%S")
#plot 2:
Sys.setlocale("LC_ALL", "English") # changing the local language to English, since the days were first in french
with(data, plot(x=fullDate, y=Global_active_power, type="l", main="",xlab="", ylab= "Global Active Power (kilowatts)" ))
# save the plot to a png file
png(filename = "plot2.png",width = 480, height = 480)
with(data, plot(x=fullDate, y=Global_active_power, type="l", main="",xlab="", ylab= "Global Active Power (kilowatts)" ))
dev.off()