setwd("F:/Business/489/Assignment 5")
tallb=read.csv("tallestbuildings.csv")
library(knitr)
library(ggplot2)
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(countrycode)
library(RColorBrewer)
opts_chunk$set(warning=FALSE, message=FALSE, fig.width=10, fig.height=5)
countcity = tallb %>% group_by(City)%>% summarise(count=length(City))
ggplot(countcity, aes(x=reorder(City,-count), y=count, xlab="Cities", ylab="City Count"))+geom_bar(stat="identity") + ggtitle("Number of Buildings by City") + theme(axis.text.x=element_text(angle = 60, hjust=1))+xlab("City")+ylab("Building Count")

heightmeter=tallb %>% group_by(City) %>% summarise(heightmetermean=mean(HeightMeter))
ggplot(countcity, aes(x=reorder(City,-heightmeter$heightmetermean), y=heightmeter$heightmetermean))+geom_bar(stat="identity") + ggtitle("Mean Height of Buildings by City") + theme(axis.text.x=element_text(angle = 60, hjust=1))+xlab("City")+ylab("Mean Height (meters)")

separate_tallb = tallb %>% separate(City, c("City", "Country"), sep="\\(")
separate_tallb$Country = gsub(")", "", as.character(separate_tallb$Country))
separate_tallb$Country = countrycode(separate_tallb$Country, "iso2c", "country.name")
countcountry = separate_tallb %>% group_by(Country)%>% summarise(count=length(Country))
ggplot(countcountry, aes(x=reorder(Country,-count), y=count))+geom_bar(stat="identity") + ggtitle("Number of Buildings by Country") + theme(axis.text.x=element_text(angle = 60, hjust=1))+xlab("Country")+ylab("Building Count")

heightmetercountry= separate_tallb %>% group_by(Country) %>% summarise(heightmetercountrymean=mean(HeightMeter))
ggplot(countcountry, aes(x=reorder(Country,-heightmetercountry$heightmetercountrymean), y=heightmetercountry$heightmetercountrymean))+geom_bar(stat="identity") + ggtitle("Mean Height of Buildings by Country") + theme(axis.text.x=element_text(angle = 60, hjust=1))+xlab("Country")+ylab("Mean Height (meters)")

ggplot(countcountry, aes(x=reorder(Country,-heightmetercountry$heightmetercountrymean), y=heightmetercountry$heightmetercountrymean, fill=as.factor(count)))+geom_bar(stat="identity") + ggtitle("Mean Height of Buildings by Country") + theme(axis.text.x=element_text(angle = 60, hjust=1))+scale_fill_brewer(palette = "Dark2", name="Building Count")+xlab("Country")+ylab("Mean Height (meters)")
