Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Course project 2 #1003

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Course Project 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

24 changes: 24 additions & 0 deletions Project2/plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#Course Project 2 (plot1)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

#calculate the total emissions
emission_year <- summarise(group_by(NEI, year), Emissions=sum(Emissions))

#using base plotting system show PM2.5 emission
plot_bar1 <- barplot(emission_year$Emissions/1000, main = "Total PM2.5 Emissions",
xlab = "Year", ylab = "PM2.5 Emissions in Kilotons",
names.arg = emission_year$year, col = "red",
ylim = c(0,8000))
text(plot_bar1, round(emission_year$Emissions/1000),
label = round(emission_year$Emissions/1000), pos = 3,
cex = 1.2)

#copy the graphic to the file
dev.copy(png, file = "plot1.png")
dev.off()
Binary file added Project2/plot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Project2/plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#Course Project 2 (plot2)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

#calculate emissions of Baltimore
emission_Balt <- summarise(group_by(subset(NEI, fips = "24510"), year),
Emissions = sum(Emissions))

#using base plotting system show PM2.5 emission of Baltimore
plot_bar2 <- barplot(emission_Balt$Emissions/1000,
main = "Total PM2.5 Emissions in Baltimore City",
xlab = "Year", ylab = "PM2.5 Emissions (Tons)",
names.arg = emission_Balt$year, col = "darkred",
ylim = c(0, 8000))
text(plot_bar2, round(emission_Balt$Emissions/1000),
label = round(emission_Balt$Emissions/1000), pos = 3,
cex = 1.2)

#copy the graphic to the file
dev.copy(png, file = "plot2.png")
dev.off()
Binary file added Project2/plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions Project2/plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#Course Project 2 (plot3)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")

#calculate emissions of Baltimore
emission_Balt <- summarise(group_by(subset(NEI, fips = "24510"), type, year),
Emissions = sum(Emissions))

#using ggplot2 to plot
ggplot(emission_Balt, aes(x = factor(year), y = Emissions, fill = type,
label = round(Emissions))) + geom_bar(stat = "identity") +
facet_grid(. ~ type) +
ggtitle("Total PM2.5 Emissions in Baltimore City") +
xlab("Year")+ ylab("PM2.5 Emissions (Tons)") +
theme(plot.title = element_text(hjust = 0.7))

#copy the graphic to the file
dev.copy(png, file = "plot3.png")
dev.off()
Binary file added Project2/plot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Project2/plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#Course Project 2 (plot4)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC_data <- readRDS("Source_Classification_Code.rds")

#calculate emissions from coal
coal_data <- grepl("Comb.*Coal", SCC_data$EI.Sector)
combustion_coal <- SCC_data[coal_data, ]
emissions_combustion <- NEI[(NEI$SCC %in% combustion_coal$SCC), ]
emissions_coal <- summarise(group_by(emissions_combustion, year),
Emissions=sum(Emissions))

#using ggplot2 to plot
ggplot(emissions_coal, aes(x=factor(year), y=Emissions/1000,fill=year,
label = round(Emissions/1000,2))) +
geom_bar(stat="identity") +
xlab("year") +
ylab(expression("total PM2.5 emissions (kilotons)")) +
ggtitle("Emissions from coal combustion-related sources (kilotons)")+
geom_label(aes(fill = year),colour = "red")

#copy the graphic to the file
dev.copy(png, file = "plot4.png")
dev.off()
Binary file added Project2/plot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions Project2/plot5.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Course Project 2 (plot5)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC_data <- readRDS("Source_Classification_Code.rds")

#calculate emissions from motor
data_motor <- SCC_data[grepl("Vehicle", SCC_data$SCC.Level.Two), ]
ssc_motor <- unique(data_motor$SCC)
emission_motor <- NEI[(NEI$SCC %in% ssc_motor), ]
motor_y <- emission_motor %>% filter(fips == "24510") %>% group_by(year) %>%
summarise(Emissions = sum(Emissions))

#using ggplot2 to plot
ggplot(motor_y, aes(factor(year), Emissions, label = round(Emissions))) +
geom_bar(stat = "identity", fill = "red") +
ggtitle("Total Motor Emissions in Baltimore City") +
xlab("Year") + ylab("PM2.5 Emissions (Tones)") +
ylim(c(0, 450)) + theme_classic()+ geom_text(size = 5, vjust = -1) +
theme(plot.title = element_text(hjust = 0.5))

#copy the graphic to the file
dev.copy(png, file = "plot5.png")
dev.off()
Binary file added Project2/plot5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions Project2/plot6.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#Course Project 2 (plot6)
#loading packages
library("ggplot2")
library("dplyr")

#read the file and take the data
NEI <- readRDS("summarySCC_PM25.rds")
SCC_data <- readRDS("Source_Classification_Code.rds")
data_motor <- SCC_data[grepl("Vehicle", SCC_data$SCC.Level.Two), ]
ssc_motor <- unique(data_motor$SCC)
emission_motor <- NEI[(NEI$SCC %in% ssc_motor), ]

#compare emmisions from motor
compare_year <- emission_motor %>% filter(fips == "24510" | fips == "06037") %>%
group_by(fips, year) %>% summarise(Emissions = sum(Emissions))
compare_year <- mutate(compare_year,
Unit = ifelse(fips == "24510", "Baltimore City",
ifelse(fips == "06037", "Los Angeles County")))

#using ggplot2 to plot
ggplot(compare_year, aes(factor(year), Emissions,
fill = Unit, label = round(Emissions))) +
geom_bar(stat = "identity") + facet_grid(. ~ Unit) +
ggtitle("Total Motor Vehicle Emissions") +
xlab("Year") + ylab("PM 2.5 Emissions in Tons") +
theme(plot.title = element_text(hjust = 0.5)) + ylim(c(0, 8000)) +
theme_classic() + geom_text(size = 4, vjust = -1)

#copy the graphic to the file
dev.copy(png, file = "plot6.png")
dev.off()
Binary file added Project2/plot6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Course Project 1
#loading and getting the data
data_raw <- read.table("household_power_consumption.txt", skip = 1,sep = ";",
na.strings = "?")
names(data_raw) <- c("Date","Time", "Global_active_power",
"Global_reactive_power", "Voltage", "Global_intensity",
"Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data_raw <- subset(data_raw, data_raw$Date=="1/2/2007" | data_raw$Date =="2/2/2007")

#plotting(1)
hist(data_raw[, 3], col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")

#copy the graphic to the file
dev.copy(png, file = "plot1.png")
dev.off()
Binary file added plot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Course Project 1
#loading and getting the data
data_raw <- read.table("household_power_consumption.txt", skip = 1,sep = ";",
na.strings = "?")
names(data_raw) <- c("Date","Time", "Global_active_power",
"Global_reactive_power", "Voltage", "Global_intensity",
"Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data_raw <- subset(data_raw, data_raw$Date=="1/2/2007" | data_raw$Date =="2/2/2007")

#change format of time data
Sys.setlocale("LC_TIME", "English")
date_format <- as.Date(data_raw$Date, format = "%d/%m/%Y")
time_format <- strptime(data_raw$Time, format = "%H:%M:%S")
time_final <- as.POSIXct(paste(date_format, data_raw$Time))

#plotting(2)
plot(time_final, data_raw$Global_active_power, xlab = "", ylab = "Global Active Power (kilowatts)", type = "l")

#copy the graphic to the file
dev.copy(png, file = "plot2.png")
dev.off()
Binary file added plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Course Project 1
#loading and getting the data
data_raw <- read.table("household_power_consumption.txt", skip = 1,sep = ";",
na.strings = "?")
names(data_raw) <- c("Date","Time", "Global_active_power",
"Global_reactive_power", "Voltage", "Global_intensity",
"Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data_raw <- subset(data_raw, data_raw$Date=="1/2/2007" | data_raw$Date =="2/2/2007")

#change format of time data
Sys.setlocale("LC_TIME", "English")
date_format <- as.Date(data_raw$Date, format = "%d/%m/%Y")
time_format <- strptime(data_raw$Time, format = "%H:%M:%S")
time_final <- as.POSIXct(paste(date_format, data_raw$Time))

#plotting(3)
plot(time_final, data_raw$Sub_metering_1, xlab = "", ylab = "Energy sub metering",
type = "l")
lines(time_final, data_raw$Sub_metering_2, col = "red")
lines(time_final, data_raw$Sub_metering_3, col = "blue")

legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black","red","blue"), lty = 1)

#copy the graphic to the file
dev.copy(png, file = "plot3.png")
dev.off()
Binary file added plot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#Course Project 1
#loading and getting the data
data_raw <- read.table("household_power_consumption.txt", skip = 1,sep = ";",
na.strings = "?")
names(data_raw) <- c("Date","Time", "Global_active_power",
"Global_reactive_power", "Voltage", "Global_intensity",
"Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data_raw <- subset(data_raw, data_raw$Date=="1/2/2007" | data_raw$Date =="2/2/2007")

#change format of time data
Sys.setlocale("LC_TIME", "English")
date_format <- as.Date(data_raw$Date, format = "%d/%m/%Y")
time_format <- strptime(data_raw$Time, format = "%H:%M:%S")
time_final <- as.POSIXct(paste(date_format, data_raw$Time))


#plotting(4)
par(mfrow = c(2,2))
plot(time_final, data_raw$Global_active_power, xlab = "",
ylab = "Global Active Power (kilowatts)",
type = "l")
plot(time_final, data_raw$Voltage, xlab = "datetime", ylab = "Voltage",
type = "l")
plot(time_final, data_raw$Sub_metering_1, xlab = "",
ylab = "Energy sub metering", type = "l")
lines(time_final, data_raw$Sub_metering_2, col = "red")
lines(time_final, data_raw$Sub_metering_3, col = "blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black","red","blue"), cex = 0.8, lty = 1 , bty = "n")
plot(time_final, data_raw$Global_reactive_power, xlab = "datetime",
ylab = "Global_reactive_power", type = "l")

#copy the graphic to the file
dev.copy(png, file = "plot4.png")
dev.off()
Binary file added plot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.