library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.3
setwd("E:/Personal/especializacion/ciencia de datos/curso4/semana4/exdata_data_NEI_data")
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
my.palette.nc <- 255
TerraCotaBluePalette <- colorRampPalette((c("#E96D63", "#7FCA9F", "#85C1F5", '#4A789C', '#FCFEFD')), interpolate = "spline")(my.palette.nc)
color <- colorRampPalette(TerraCotaBluePalette)
emissions <- merge(NEI,SCC, by="SCC")
Have total emissions from PM2.5 decreased in the United States from 1999 to 2008? Using the base plotting system, make a plot showing the total PM2.5 emission from all sources for each of the years 1999, 2002, 2005, and 2008
##EmisionsByYear: sum emissions by year
emissionsByYear<- tapply(emissions$Emissions, emissions$year, sum)
barplot (emissionsByYear, col = c(color(5)), main="Fine Particulate Emissions \n" , xlab="Years", ylab="Total (tons) of PM2.5")
Total PM2.5 emissions have decreased from 1999 to 2005 in United States
Have total emissions from PM2.5 decreased in the Baltimore City, Maryland (fips == “24510”) from 1999 to 2008? Use the base plotting system to make a plot answering this question.
emissionBalt <- subset(emissions, fips == "24510")
emissionsByYearBalt<- tapply(emissionBalt$Emissions, emissionBalt$year, sum)
barplot (emissionsByYearBalt,col = c(color(5)), main="Fine Particulate Emissions in Baltimore \n" , xlab="Years", ylab="Total (tons) of PM2.5")
there was a decrease from 1999 to 2002, went up in 2005 and down again in 2008
Of the four types of sources indicated by the type (point, nonpoint, onroad, nonroad) variable, which of these four sources have seen decreases in emissions from 1999–2008 for Baltimore City? Which have seen increases in emissions from 1999–2008? Use the ggplot2 plotting system to make a plot answer this question.
baltimoreByType <- aggregate(Emissions ~ year + type, emissionBalt, sum)
ggplot(baltimoreByType, aes(year, Emissions, col = type)) +
geom_line() +
geom_point() +
ggtitle(expression("Total Baltimore PM2.5 Emissions by Type and Year")) +
ylab(expression("Total Baltimore PM2.5 Emissions")) +
xlab("Year") +
scale_colour_discrete(name = "Type of sources") +
theme(legend.title = element_text(face = "bold"))
It is graphically observed that the types of NONROAD , NONPOINT , ONROAD sources decreased in total emissions; the POINT source increased in emissions until 2005 and then decreased until 2008
Across the United States, how have emissions from coal combustion-related sources changed from 1999–2008?
## buscamos las filas relacionadas con coal
emissionsCoal <- emissions[grepl("coal", emissions$Short.Name, ignore.case = T),]
##group total by year and type
emissionType <- aggregate(Emissions ~ year + type, emissionsCoal, sum)
## graphic
ggplot(emissionType, aes(year, Emissions, col = type)) +
geom_line() +
geom_point() +
ggtitle(expression("Total Emmision ( PM2.5 ) by Type and year from Coal source in United States")) +
ylab(expression("Total Emissions (PM2.5)")) +
xlab("Year") +
scale_colour_discrete(name = "Type of sources") +
theme(legend.title = element_text(face = "bold"))
In the United States emissions from coal combustion sources decreased for point type between 1999 and 2008 and for you NONPOINT increased from 1999 to 2002, decreased from 2005 to 2008 remained between 2002 and 2005
How have emissions from motor vehicle sources changed from 1999–2008 in Baltimore City?
emissionsVehMotor <- emissionBalt[ grepl("Motor vehicle", emissionBalt$Short.Name, ignore.case = T),] ## buscamos las filas relacionadas con coal
baltVehMotorSum<- tapply(emissionsVehMotor$Emissions, emissionsVehMotor$year, sum)
barplot (baltVehMotorSum, col = c(color(5)), main="Fine Particulate Emissions for Baltimore from vehicle motor sources \n" , xlab="Years", ylab="Total (tons) of PM2.5")
It is observed that there were emissions in 2002 and 2005 in Baltimore from sources of motor vehicles, reach 10 tons of PM2.5
Compare emissions from motor vehicle sources in Baltimore City with emissions from motor vehicle sources in Los Angeles County, California (fips == “06037”). Which city has seen greater changes over time in motor vehicle emissions?
emissionBaltAng <- subset(emissions, fips == "24510" | fips == "06037" )
emissionsBaltAngVehMotor <- emissionBaltAng[ grepl("Motor vehicle", emissionBaltAng$Short.Name, ignore.case = T),] ## buscamos las filas relacionadas con
baltAngVehMotorSum <- aggregate(Emissions ~ year + type + fips, emissionsBaltAngVehMotor, sum)
ggplot(baltAngVehMotorSum, aes(year, Emissions, col = fips)) +
geom_line() +
geom_point() +
ggtitle(expression("Total Emissions ( PM2.5 ) by City and Year for Baltimore vs Angeles")) +
ylab(expression("Total PM2.5 Emissions (tons)")) +
xlab("Year") +
scale_colour_discrete(name = "City") +
theme(legend.title = element_text(face = "bold"))
Emissions from motor vehicles in los Angeles remained constant in Baltimore at 10 tons of MP2.5 , but there were increases in Los Angeles from 61.4 tons of PM2. 5 to 46.73 tons of PM2.5