library(tidyr)
library(ggplot2)
library(readxl)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  2.1.3     ✓ dplyr   0.8.3
## ✓ readr   1.3.1     ✓ stringr 1.4.0
## ✓ purrr   0.3.3     ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(countrycode)
library(readxl)
url <- "http://patilv.com/gozagsdatavizsp20/data/tallestbuildings.xlsx"
destfile <- "tallestbuildings.xlsx"
curl::curl_download(url, destfile)
tallestbuildings <- read_excel(destfile)
tallest2 <- tallestbuildings %>% 
  separate(City, into = c("City", "CountryCode"), sep = "[()]")
## Warning: Expected 2 pieces. Additional pieces discarded in 100 rows [1, 2, 3, 4,
## 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...].

1. Buildings per City

tallest2 %>%
    group_by(City) %>% summarise(Building_Count = n()) %>%
    arrange(desc(Building_Count)) %>%
    ggplot(aes(y=Building_Count, x=reorder(City, Building_Count))) +
    geom_bar(stat = "identity") + coord_flip()

2. Buildings in Order of Mean Height

tallest2 %>% group_by(City) %>%
  summarise(Mean.Height=mean(`Height (ft)`)) %>%
  ggplot(aes(x= reorder(City,Mean.Height),y=Mean.Height)) +
  geom_bar(stat = "identity") + coord_flip()

Changing Country Code to Name

tallest2$CountryCode = countrycode(tallest2$CountryCode,"iso2c","country.name")

1a. (1) Grouped By Country

tallest2 %>%
    group_by(CountryCode) %>% summarise(Building_Count = n()) %>%
    arrange(desc(Building_Count)) %>%
    ggplot(aes(y=Building_Count, x=reorder(CountryCode, Building_Count))) +
    geom_bar(stat = "identity") + coord_flip()

2b. (2) Grouped By Country

tallest2 %>% group_by(CountryCode) %>%
  summarise(Mean.Height=mean(`Height (ft)`)) %>%
  ggplot(aes(x= reorder(CountryCode,Mean.Height),y=Mean.Height)) +
  geom_bar(stat = "identity") + coord_flip()

count = tallest2 %>% group_by(CountryCode) %>% summarise(Building_Count = n(),  Mean.Height=mean(`Height (ft)`))
tallest2 %>% group_by(CountryCode) %>%
  summarise(Mean.Height=mean(`Height (ft)`)) %>%
  ggplot(aes(x= reorder(CountryCode,Mean.Height),y=Mean.Height, fill(Building_Count))) +
  geom_bar(stat = "identity", position = "dodge") + coord_flip() + scale_fill_brewer(palette="Paired")

uses = tallest2 %>% group_by(Use) %>%
  summarise(Mean.Height=mean(`Height (ft)`))
uses %>%
  ggplot(aes(x= reorder(Use,Mean.Height),y=Mean.Height)) +
  geom_bar(stat = "identity",position="dodge") + coord_flip()