Exploratory Data Analysis , week (1-4) All Quiz Answers with Assignment .

Share:
Exploratory Data Analysis

Week 1 Assignment :

https://github.com/a-tagliente/ExData_Plotting1

Week 4 Assignment :\

1. 


PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds')) } # Create the png png('./plot1.png', width = 500, height = 450, res = 55, type = 'cairo') # default is 480px X 480px # Make Plot with(aggregate(Emissions ~ year, NEI, sum), plot(Emissions~year, pch = 18, xlab = '', ylab = 'Total PM2.5 Emissions (tons)', main = 'Total PM2.5 Emissions by Year', col = "blue", type = "b", xlim = c(1999, 2008), lty = 2, lwd = 1.5, lab = c(10, 5, 7))) # Close png file dev.off()



2.


PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds')) } # Create the png png('./plot2.png', width = 500, height = 450, res = 55, type = 'cairo') # default is 480px X 480px # subset data NEI_Baltimore <- subset(NEI, fips == "24510") # Make Plot with(aggregate(Emissions ~ year, NEI_Baltimore, sum), plot(Emissions~year, pch = 18, xlab = '', ylab = 'Total PM2.5 Emissions (tons)', main = 'Total PM2.5 Emissions by Year in Baltimore City', col = "blue", type = "b", xlim = c(1999, 2008), lty = 2, lwd = 1.5, lab = c(10, 5, 7))) # Close png file dev.off()


3.


PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds')) } # Create the png png('./plot3.png', width = 700, height = 370, res = 75, type = 'cairo') # default is 480px X 480px # subset data NEI_Baltimore <- subset(NEI, fips == "24510") # Make Plot NEI_Baltimore %>% group_by(year, type) %>% summarise(sum = sum(Emissions)) %>% ggplot(aes(year, sum)) + geom_point() + geom_line() + facet_wrap(~type, ncol = 4) + labs(title = 'Total PM2.5 Emission by Year in Baltimore City', subtitle = 'Subsetted by Type of Source') + xlab('') + ylab('Total PM2.5 Emission (tons)') + scale_x_continuous(breaks = unique(NEI_Baltimore$year)) + theme_bw() # Close png file dev.off()



4. 


PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds')) } # Create the png png('./plot4.png', width = 700, height = 370, res = 75, type = 'cairo') # default is 480px X 480px # merge data and make Plot NEI %>% inner_join(SCC, by = 'SCC') %>% dplyr::filter(str_detect(Short.Name, '[Cc]oal')) %>% group_by(year, type) %>% summarise(sum = sum(Emissions)) %>% ggplot(aes(year, sum)) + geom_point() + geom_line() + facet_wrap(~type, ncol = 4) + labs(title = 'Total PM2.5 Coal Emission by Year', subtitle = 'Subsetted by Type of Source') + xlab('') + ylab('Total PM2.5 Emission (tons)') + scale_x_continuous(breaks = unique(NEI$year)) + theme_bw() # Close png file dev.off()


5.


PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC_PM25.rds', 'Source_Classification_Code.rds')) } # Create the png png('./plot5.png', width = 700, height = 370, res = 75, type = 'cairo') # default is 480px X 480px # Make Plot NEI %>% dplyr::filter(fips == '24510', type == 'ON-ROAD') %>% group_by(year) %>% summarise(sum = sum(Emissions)) %>% ggplot(aes(year, sum)) + geom_point() + geom_line() + labs(title = 'Total PM2.5 Emission by Year in Baltimore City', subtitle = 'Subsetted from Motor Vehicle Sources ("On- Road type")') + xlab('') + ylab('Total PM2.5 Emission (tons)') + scale_x_continuous(breaks = unique(NEI$year)) + theme_bw() # Close png file dev.off()


6. 
















PROMPT

Copy and paste the R code file for the plot uploaded in the previous question.

# libraries require(utils) require(cairoDevice) # anti-aliasing figure # download, load data and subset fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata %2FNEI_data.zip' download.file(fileUrl, destfile = './Data.zip', method = 'curl', quiet = T) if(file.exists('./Data.zip')) { # Extract data file unzip('./Data.zip') # Delete original Zip file if it exists invisible(file.remove('./Data.zip')) } ## This first line will likely take a few seconds. Be patient! NEI <- readRDS("summarySCC_PM25.rds") SCC <- readRDS("Source_Classification_Code.rds") # Delete data files .rds if(all(file.exists('./summarySCC_PM25.rds', 'Source_Classification_Code.rds'))) { invisible(file.remove('./summarySCC PM25.rds' 'Source_Classification_Code.rds')) } # Create the png png('./plot6.png', width = 700, height = 370, res = 75, type = 'cairo') # default is 480px X 480px # Make Plot NEI %>% dplyr::filter(fips %in% c("24510", "06037"), type == 'ON-ROAD') %>% group_by(year, fips) %>% summarise(sum = sum(Emissions)) %>% ggplot(aes(year, sum, col = fips)) + geom_point() + geom_line() + labs(title = 'Total PM2.5 Emission by Year in Baltimore and Los Angeles', subtitle = 'Subsetted from Motor Vehicle Sources ("On- Road type")') + xlab('') + ylab('Total PM2.5 Emission (tons)') + scale_colour_discrete(name = "City", labels = c("Los Angeles", "Baltimore")) + theme(legend.title = element_text(face = "bold")) + scale_x_continuous(breaks = unique(NEI$year)) + theme_bw() # Close png file dev.off()















No comments