setwd("C:/Users/Joey/OneDrive/Documents/MBUS 699 - Data Viz/MYWORK/Tallest Building Assignment")
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(tidyr)
library(ggplot2)
library(countrycode)
TB=read.csv("TB.csv")

Q1: Building vs City

Q1=TB%>%group_by(City)%>%summarize(Count=length(City))

ggplot(Q1, aes(x = reorder(City, Count), y = Count)) + geom_bar(stat = "identity") + coord_flip()

Q2: HeightMeter (mean) of Building vs City

Q2=TB%>%group_by(City)%>%summarize(mean=mean(HeightMeter))

ggplot(Q2, aes(x = reorder(City, mean), y = mean)) + geom_bar(stat = "identity") + coord_flip() + ylab("Mean Height (meters)") +xlab("City")

Q3:Building vs Country

TB$city = as.character(TB$City)
TB$City = gsub(")","",TB$City)
TB$City = gsub( "\\(","Hello",TB$City)
Q3=TB%>%separate(City, c("City", "Country"), sep = "Hello")%>%group_by(Country)%>%summarize(Count=length(Country))
Q3$Country=countrycode(Q3$Country,"iso2c", "country.name")

ggplot(Q3, aes(x = reorder(Country, Count), y = Count)) + geom_bar(stat = "identity") + coord_flip()

Q4: MeanHeight vs Country

TB$city = as.character(TB$City)
TB$City = gsub(")","",TB$City)
TB$City = gsub( "\\(","Hello",TB$City)
Q4=TB%>%separate(City, c("City", "Country"), sep = "Hello")%>%group_by(Country)%>%summarize(mean=mean(HeightMeter))
Q4$Country=countrycode(Q4$Country,"iso2c", "country.name")


ggplot(Q4, aes(x = reorder(Country, mean), y = mean)) + geom_bar(stat = "identity") + coord_flip() + ylab("Mean Height (meters)") +xlab("Country")

Q5: Color Gradiante - MeanHeight vs Country

TB$city = as.character(TB$City)
TB$City = gsub(")","",TB$City)
TB$City = gsub( "\\(","Hello",TB$City)
Q5=TB%>%separate(City, c("City", "Country"), sep = "Hello")%>%group_by(Country)%>%summarize(mean=mean(HeightMeter))
Q5$Country=countrycode(Q5$Country,"iso2c", "country.name")


ggplot(Q5, aes(x = reorder(Country, mean), y = mean, fill=Country)) + geom_bar(stat = "identity") + coord_flip() + ylab("Mean Height (meters)") +xlab("Country")