diff --git a/Course Project 2 b/Course Project 2 new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/Course Project 2 @@ -0,0 +1 @@ + diff --git a/Project2/plot1.R b/Project2/plot1.R new file mode 100644 index 00000000000..ed4702110bc --- /dev/null +++ b/Project2/plot1.R @@ -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() diff --git a/Project2/plot1.png b/Project2/plot1.png new file mode 100644 index 00000000000..70fb96f37fa Binary files /dev/null and b/Project2/plot1.png differ diff --git a/Project2/plot2.R b/Project2/plot2.R new file mode 100644 index 00000000000..bc2d49d7bb0 --- /dev/null +++ b/Project2/plot2.R @@ -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() \ No newline at end of file diff --git a/Project2/plot2.png b/Project2/plot2.png new file mode 100644 index 00000000000..e8401eebfc1 Binary files /dev/null and b/Project2/plot2.png differ diff --git a/Project2/plot3.R b/Project2/plot3.R new file mode 100644 index 00000000000..d25e8e34bb5 --- /dev/null +++ b/Project2/plot3.R @@ -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() \ No newline at end of file diff --git a/Project2/plot3.png b/Project2/plot3.png new file mode 100644 index 00000000000..12e36fe20eb Binary files /dev/null and b/Project2/plot3.png differ diff --git a/Project2/plot4.R b/Project2/plot4.R new file mode 100644 index 00000000000..b0e5688cc6d --- /dev/null +++ b/Project2/plot4.R @@ -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() \ No newline at end of file diff --git a/Project2/plot4.png b/Project2/plot4.png new file mode 100644 index 00000000000..daaf5bad076 Binary files /dev/null and b/Project2/plot4.png differ diff --git a/Project2/plot5.R b/Project2/plot5.R new file mode 100644 index 00000000000..75a035268ce --- /dev/null +++ b/Project2/plot5.R @@ -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() \ No newline at end of file diff --git a/Project2/plot5.png b/Project2/plot5.png new file mode 100644 index 00000000000..9e30dac2ed8 Binary files /dev/null and b/Project2/plot5.png differ diff --git a/Project2/plot6.R b/Project2/plot6.R new file mode 100644 index 00000000000..f5491a9bab3 --- /dev/null +++ b/Project2/plot6.R @@ -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() \ No newline at end of file diff --git a/Project2/plot6.png b/Project2/plot6.png new file mode 100644 index 00000000000..9a71275df6c Binary files /dev/null and b/Project2/plot6.png differ diff --git a/plot1.R b/plot1.R new file mode 100644 index 00000000000..d561dc0688a --- /dev/null +++ b/plot1.R @@ -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() \ No newline at end of file diff --git a/plot1.png b/plot1.png new file mode 100644 index 00000000000..07efb4ae77a Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 00000000000..b6553eee171 --- /dev/null +++ b/plot2.R @@ -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() diff --git a/plot2.png b/plot2.png new file mode 100644 index 00000000000..4f9d1f7d27b Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 00000000000..94db1a29c9e --- /dev/null +++ b/plot3.R @@ -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() \ No newline at end of file diff --git a/plot3.png b/plot3.png new file mode 100644 index 00000000000..2f5ba029536 Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 00000000000..249baabe2c0 --- /dev/null +++ b/plot4.R @@ -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() \ No newline at end of file diff --git a/plot4.png b/plot4.png new file mode 100644 index 00000000000..50cda3a9d4d Binary files /dev/null and b/plot4.png differ