forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.r
75 lines (51 loc) · 2.41 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
## course project 1
## install and library necessary packages
## data.tables package fread() command can help us load data faster than
## read.table() function
if (!suppressWarnings(require(data.table)))
{
install.packages("data.table")
require(data.table)
}
if(!suppressWarnings(require(dplyr)))
{
install.packages("dplyr")
require(dplyr)
}
## download household_power_comsumption compressed dataset
data_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
if (!dir.exists("course_project1")) dir.create("./course_project1")
## I don't why i can't use this url to download dataset as before!!
## if you can't download dataset like this,please download by yourself through web
download.file(data_url,destfile = "./course_project1/dataset.zip")
setwd("./course_project1")
## uncompressing and loading data to your R console
##when you use list.file() command in unzip() function , please check
## the work directory only have one file(your dataset: .zip file)
## when you use fread() function to load dataset, you will get warning message
## this warning message is caused by missing data "?"
unzip(list.files(),exdir=".")
HPC <- fread("household_power_consumption.txt")
## subset specific data to new stored
HPC_sub <- subset(HPC, Date == "1/2/2007" | Date == "2/2/2007")
HPC_sub <- transform(HPC_sub,Date_Time = paste(Date,Time,sep=" "))
HPC_sub <- HPC_sub[,-c(1:2)]
HPC_sub <- select(HPC_sub,Date_Time,1:7)
## change date_Time variable format for plot2,3,4
date_time <-strptime(HPC_sub$Date_Time,format="%d/%m/%Y %H:%M:%S")
date_time <- as.data.frame(date_time)
HPC_sub$Date_Time <- date_time[,1]
## change the data format for plot1
HPC_sub$Global_active_power <- as.numeric(HPC_sub$Global_active_power)
## change the data format for plot3
HPC_sub$Sub_metering_1 <- as.numeric(HPC_sub$Sub_metering_1)
HPC_sub$Sub_metering_2 <- as.numeric(HPC_sub$Sub_metering_2)
HPC_sub$Sub_metering_3 <- as.numeric(HPC_sub$Sub_metering_3)
## plotting by plot() function and Various parameters
## my graphic xlab is simplifed chinese,not english "Thu","Fri" and "Sat"
## And i don't how to change it
png(file ="./plot2.png",width=480,height = 480)
with(HPC_sub,plot(Date_Time,Global_active_power,type = "l",
xlab="",ylab="Global Active Power(Kilowatts)"))
## close the graphic device
dev.off()