forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.r
61 lines (50 loc) · 1.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## Set the path to the data directory
print(dir.data <- paste0(getwd(),"/"))
## Make a variable to hold the path to the file we want to import
print(path.abs.data.src <- paste0(
dir.data
,"household_power_consumption.txt"
))
## Confirm we have downloaded the data properly
stopifnot(file.exists(path.abs.data.src))
## Import the data to R
head(power.consumption <- read.table(
path.abs.data.src
,sep = ";"
,header = TRUE
,na.string = "?"
))
## Briefly explore to see how the data imported
str(power.consumption)
summary(power.consumption)
## Convert the dates to actual dates
power.consumption$DateTime <- strptime(paste(power.consumption$Date,power.consumption$Time), format = "%d/%m/%Y %H:%M:%S")
power.consumption$DateAsDate <- as.Date(power.consumption$Date, format = "%d/%m/%Y")
## Check the structure post conversion
str(power.consumption)
## Limit to the data we want to plot
power.consumption.limit <- subset(power.consumption
,power.consumption$DateAsDate == as.Date("2007-02-01")
| power.consumption$DateAsDate == as.Date("2007-02-02")
)
## Confirm our limitations worked
print(unique(power.consumption.limit$DateAsDate))
## See the list of named (red) colors available. Useful for development
# print(grep("^red"
# ,colors()
# ,ignore.case = TRUE
# , value = TRUE
# )
# )
## Make the actual plot
png("plot1.png"
,width = 480
,height = 480
,units = "px"
)
hist(power.consumption.limit$Global_active_power
,main = "Global Active Power"
,xlab = "Global Active Power (kilowatts)"
,col = "red"
)
dev.off()