-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot3.R
More file actions
95 lines (73 loc) · 3.92 KB
/
plot3.R
File metadata and controls
95 lines (73 loc) · 3.92 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
################################################################################
# #
# Specialization: Data Science - Foundations using R Specialization #
# Course: Exploratory Data Analysis #
# #
# Author: Anderson Hitoshi Uyekita #
# Date: 2022/06/17 #
# #
# Course Project: EPA National Emissions Inventory (Week 4) #
# Deliverable: plot3.R #
# #
################################################################################
########################### Libraries Requirements #############################
library(ggplot2)
library(magrittr)
library(tidyverse)
########################### 1. Creating a folder ###############################
# 1. Create a data directory
if(!base::file.exists("data")) {
base::dir.create("data")
}
########################### 2. Downloading data ################################
# 2. Download files and store it in data directory.
if(!base::file.exists("./data/FNEI_data.zip")){
utils::download.file(url = "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip",
destfile = "./data/FNEI_data.zip")
}
# 2.1. Unzipping the FNEI_data.zip file.
if(!base::file.exists("./data/unzipped/Source_Classification_Code.rds") | !base::file.exists("./data/unzipped/summarySCC_PM25.rds")){
utils::unzip(zipfile = "./data/FNEI_data.zip",
exdir = "./data/unzipped/",
list = FALSE,
overwrite = TRUE)
}
########################### 3. Loading RDS files ###############################
# 3. Loading the RDS files.
NEI <- base::readRDS("./data/unzipped/summarySCC_PM25.rds")
SCC <- base::readRDS("./data/unzipped/Source_Classification_Code.rds")
########################### 4. Dataset Manipulation ############################
# 4.1. Creating a subsetting for Baltimore City.
NEI_q3 <- base::subset(x = NEI, NEI$fips == "24510")
# 4.2. Summarizing the data set to calculate the total summation by year and type.
plot_3_data <- NEI_q3 %>%
dplyr::group_by(type, year) %>%
dplyr::summarise(Total = base::sum(Emissions))
#################################### 5. Plot 3 #######################################
# 5.1. Creating a PNG file.
grDevices::png(filename = "plot3.png", height = 480, width = 800)
# Plotting a GGPLOT2 graphic.
ggplot2::ggplot(data = plot_3_data,
ggplot2::aes(x = year,
y = Total,
label = base::format(x = Total,
nsmall = 1,
digits = 1))) +
# Defining a line graphic.
ggplot2::geom_line(ggplot2::aes(color = type), lwd = 1) +
# Adding labels to each point.
ggplot2::geom_text(hjust = 0.5, vjust = 0.5) +
# Setting the years.
ggplot2::scale_x_discrete(limits = c(1999, 2002, 2005, 2008)) +
# Editing the Graphic Tile.
ggplot2::labs(title = base::expression('Emissions of PM'[2.5] ~ ' in Baltimore')) +
# Adding x-axis label.
ggplot2::xlab("Year") +
# Adding y-axis label.
ggplot2::ylab(base::expression("Total PM"[2.5] ~ "emission (tons)")) +
# Editing the legend position and tile position.
ggplot2::theme(legend.position = "right",
legend.title.align = 0.5,
plot.title = ggplot2::element_text(hjust = 0.5))
# Closing the device.
grDevices::dev.off()