## download the file and unzip
##download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip" , "Emissions.zip" )
library(ggplot2)
dir.create("Emissions")
## Warning in dir.create("Emissions"): 'Emissions' already exists
if(file.exists("Emissions.zip") & dir.exists("Emissions"))
{
unzip("Emissions.zip" , exdir = ".//Emissions")
}
## Read the files
dfNEI <- readRDS("Emissions/summarySCC_PM25.rds")
dfSCC <- readRDS("Emissions/Source_Classification_Code.rds")
#1
dfEmissionSum <- aggregate(dfNEI$Emissions , by = list(dfNEI$year) , FUN = sum)
png(filename ="plot1.png")
barplot( height = dfEmissionSum$x ,col = "orange" , width = 20 ,
xlab = "Year" , ylab =" Total Emissions" , main = "Total emissions per year",
ylim = range(0,8000000) , axisnames = TRUE , names.arg = dfEmissionSum$Group.1,
space = 1)
dev.off()
## png
## 2
#2
dfBalt <- subset(dfNEI , fips == "24510")
dfEmBalt <- aggregate(dfBalt$Emissions,by = list(dfBalt$year) , FUN = sum)
png(filename ="plot2.png")
barplot( height = dfEmBalt$x ,col = "lightgreen" , width = 20 ,
xlab = "Year" , ylab =" Total Emissions" , main = "Total emissions per year for Baltimore",
ylim = range(0,4000) , axisnames = TRUE
, names.arg = dfEmBalt$Group.1,
space = 1)
dev.off()
## png
## 2
#3
dfBalt <- subset(dfNEI , fips == "24510")
dfEmTypeBalt <- aggregate(dfBalt$Emissions,by = list(dfBalt$year , dfBalt$type)
, FUN = sum)
gBaltEmm <- ggplot(dfEmTypeBalt , aes(Group.1 , x , colour = Group.2 , shape = Group.2)) +
xlab("Year")+
ylab("Emissions") +
ylim(0, 2500) +
ggtitle("Emissions per year of each type" )+
geom_line() +
geom_point() +
scale_color_discrete(name = "Emission Type")+
scale_shape_discrete(name = "Emission Type")
gBaltEmm

ggsave("plot3.png" , device = "png" , scale =1 , dpi = 500 , width = 6 , height = 6)
#4
#Get all SCC Ids related to coal
dCoal <- subset(dfSCC , grepl("Coal" , dfSCC$Short.Name)
| grepl("coal" , dfSCC$Short.Name))$SCC
dfCoal <- dfNEI[dfNEI$SCC %in% dCoal , c(4,6)]
dfCoal <- aggregate(dfCoal$Emissions , by = list(dfCoal$year) , FUN = sum)
png("plot4.png")
plot( y = dfCoal$x , x = dfCoal$Group.1 , col = "lightblue" , type = "l",
xlab = "Year" , ylab =" Total Emissions" , main = "Total emissions per year for coal",
ylim = range(300000,603000) , lwd = 2 , axes = FALSE)
axis(side = 1 , at= seq(1999, 2008, 3) , labels = dfCoal$Group.1 )
axis(side = 2 , at= seq(300000,603000 , 100000) )
points(y = dfCoal$x , x = dfCoal$Group.1 , col = "lightblue" , pch = 16 )
dev.off()
## png
## 2
#5
dMotor <- subset(dfSCC , grepl("vehicle" , dfSCC$Short.Name)
| grepl("Vehicle" , dfSCC$Short.Name))$SCC
dfMotor <- dfNEI[dfNEI$SCC %in% dMotor & dfNEI$fips == "24510" , c(4,6)]
dfMotor <- aggregate(dfMotor$Emissions , by = list(dfMotor$year) , FUN = sum)
png("plot5.png")
plot( y = dfMotor$x , x = dfMotor$Group.1 , col = "green" , type = "l",
xlab = "Year" , ylab =" Total Emissions" , main = "Total emissions per year for vehicles in Baltimore",
ylim = range(0,250) , lwd = 2 , axes = FALSE)
axis(side = 1 , at= seq(1999, 2008, 3) , labels = dfMotor$Group.1 )
axis(side = 2 , at= seq(0, 250 , 20) )
points(y = dfMotor$x , x = dfMotor$Group.1 , col = "green" , pch = 16 )
dev.off()
## png
## 2
# 6
dfCounty <- dfNEI[dfNEI$fips == "06037" | dfNEI$fips == "24510" , c(1,4,6)]
dfCounty <- aggregate(dfCounty$Emissions , by = list(dfCounty$year , dfCounty$fips)
, FUN = sum)
gCounty <- ggplot(dfCounty , aes(Group.1 , x , colour = Group.2 , shape = Group.2)) +
xlab("Year")+
ylab("Emissions") +
ylim(0, 47500) +
ggtitle("Vehicle emissions per year of each country" )+
geom_line() +
geom_point() +
scale_color_discrete(name = "County"
, labels = c("Los Angeles County" , "Baltimore City"))+
scale_shape_discrete(name = "County"
, labels = c("Los Angeles County" , "Baltimore City"))
gCounty

ggsave("plot6.png" , device = "png" , scale =1 , dpi = 500 , width = 6 , height = 6)