setwd("/Users/cademiller/Desktop/DataViz")
suppressPackageStartupMessages(library(ggmap))
## Warning: package 'ggmap' was built under R version 3.2.2
suppressPackageStartupMessages(library(dplyr))
## Warning: package 'dplyr' was built under R version 3.2.2
suppressPackageStartupMessages(library(tidyr))
## Warning: package 'tidyr' was built under R version 3.2.2
suppressPackageStartupMessages(library(maps))
suppressPackageStartupMessages(library(maptools))
## Warning: package 'sp' was built under R version 3.2.2
suppressPackageStartupMessages(library(countrycode))
suppressPackageStartupMessages(library(choroplethr))
suppressPackageStartupMessages(library(choroplethrMaps))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(rgdal))
suppressPackageStartupMessages(library(rgeos))
## Warning: package 'rgeos' was built under R version 3.2.2
suppressPackageStartupMessages(library(htmlwidgets))
suppressPackageStartupMessages(library(htmltools))
suppressPackageStartupMessages(library(leaflet))
suppressPackageStartupMessages(library(sp))
suppressPackageStartupMessages(library(googleVis))
## Warning: package 'googleVis' was built under R version 3.2.2
TB=read.csv("TB.csv")
TB$City=gsub("\xa0","",TB$City)
TB$City =gsub ("\xao","", as.character(TB$City))
Q1INT=TB%>%separate(City, c("City", "Country"), sep = "\\(")
Q1INT$Country=gsub(")","", as.character(Q1INT$Country))
Q1INT$Country=countrycode(Q1INT$Country,"iso2c", "country.name")
Q1INT$City=gsub("\xca","",as.character(Q1INT$City))
locs=structure(list(lon = c(54.36667, -84.38798, 100.50144, 116.39723,
129.04028, -87.65005, 106.55278, 51.52245, 55.30472, 153.43088,
113.25, 105.84117, 114.15769, -95.36327, 126.73167, 120.26302,
116.99722, 120.31333, 101.68653, 47.97833, -0.1278328, -118.24368,
39.82611, 144.96332, 37.5, 115.88333, 118.77778, -73.992602,
135.50218, 46.72185, -70.64827, 121.45806, 123.43278, 114.0683,
121.53185, 117.17667, -79.4163, 120.66682, 114.26667, 120.28857
), lat = c(24.46667, 33.749, 13.75398, 39.9075, 35.10278, 41.85003,
29.56278, 25.27932, 25.25817, -28.00029, 23.11667, 21.0245, 22.28552,
29.76328, 37.45361, 31.91102, 36.66833, 22.61626, 3.1412, 29.36972,
51.5072648, 34.05223, 21.42667, -37.814, 55.75, 28.68333, 32.06167,
40.742185, 34.69374, 24.68773, -33.45694, 31.22222, 41.79222,
22.54554, 25.04776, 39.14222, 43.70011, 27.99942, 30.58333, 31.56887
), City = c("Abu Dhabi ", "Atlanta ", "Bangkok ", "Beijing ",
"Busan ", "Chicago ", "Chongqing ", "Doha ", "Dubai ", "Gold Coast ",
"Guangzhou ", "Hanoi ", "Hong Kong ", "Houston ", "Incheon ",
"Jiangyin ", "Jinan ", "Kaohsiung ", "Kuala Lumpur ", "Kuwait City ",
"London ", "Los Angeles ", "Mecca ", "Melbourne ", "Moscow ",
"Nanchang ", "Nanjing ", "New York City ", "Osaka ", "Riyadh ",
"Santiago ", "Shanghai ", "Shenyang ", "Shenzhen ", "Taipei ",
"Tianjin ", "Toronto ", "Wenzhou ", "Wuhan ", "Wuxi ")), .Names = c("long",
"lat", "City"), row.names = c(NA, -40L), class = "data.frame")
Q1FIN=Q1INT%>%group_by(City)%>%dplyr::summarize(Count=length(City))
locs$Count=Q1FIN$Count
mapWorld <- borders("world", colour="grey50", fill="lightgrey")
mp <- ggplot() + mapWorld
#mp <- mp + geom_polygon( data=locs, aes(x=long, y=lat, group = group),colour="white" )
mp <- mp + geom_point(data=locs, aes(x=long, y=lat, color=as.factor(Count)),size=3) + ggtitle("Buildings Per City")
plot(mp)
locs$popup=paste("<table><tr><td>City:", locs$City, "<br>Number of Buildings in City:", locs$Count,"</td></tr></table>")
#topcities <-
leaflet(locs)%>%
addProviderTiles("CartoDB.Positron") %>%
setView(0, 0, zoom = 2) %>%
addCircles(stroke=FALSE, fillOpacity = .6, color= "blue", radius=~Count*5000,popup=~popup)
## Assuming 'long' and 'lat' are longitude and latitude, respectively
#print(topcities)
Static Choropleth
Q2FIN=Q1INT%>%group_by(Country)%>%dplyr::summarize(Count=length(Country))
Q2FIN$Country[Q2FIN$Country == "Russian Federation"] <- "Russia"
Q2FIN$Country[Q2FIN$Country == "Viet Nam"] <- "Vietnam"
Q2FIN$Country[Q2FIN$Country == "Korea, Republic of"] <- "South Korea"
Q2FIN$Country[Q2FIN$Country == "United States"] <- "United States of America"
Q2FIN$Country[Q2FIN$Country == "Taiwan, Province of China"] <- "Taiwan"
Q2FIN$Country=tolower(c(Q2FIN$Country))
dfQ2FIN=Q2FIN[,c(1,2)]
colnames(dfQ2FIN)=c("region","value")
country_choropleth(dfQ2FIN, title="Buildings By Country")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: afghanistan, angola, azerbaijan, moldova, madagascar, mexico,
## macedonia, mali, myanmar, montenegro, mongolia, mozambique, mauritania,
## burundi, malawi, namibia, france, niger, nigeria, nicaragua, netherlands,
## norway, nepal, belgium, new zealand, oman, pakistan, panama, peru,
## philippines, papua new guinea, poland, north korea, benin, portugal,
## paraguay, israel, romania, rwanda, western sahara, sudan, burkina
## faso, south sudan, senegal, solomon islands, sierra leone, el salvador,
## somaliland, somalia, republic of serbia, suriname, slovakia, bangladesh,
## slovenia, sweden, swaziland, syria, chad, togo, tajikistan, turkmenistan,
## east timor, bulgaria, trinidad and tobago, tunisia, turkey, united
## republic of tanzania, uganda, ukraine, uruguay, uzbekistan, the bahamas,
## venezuela, vanuatu, yemen, south africa, zambia, zimbabwe, bosnia and
## herzegovina, belarus, albania, belize, bolivia, brazil, brunei, bhutan,
## botswana, central african republic, switzerland, ivory coast, cameroon,
## democratic republic of the congo, republic of congo, colombia, costa
## rica, cuba, northern cyprus, cyprus, argentina, czech republic, germany,
## djibouti, denmark, dominican republic, algeria, ecuador, egypt, eritrea,
## spain, armenia, estonia, ethiopia, finland, fiji, gabon, georgia, ghana,
## antarctica, guinea, gambia, guinea bissau, equatorial guinea, greece,
## guatemala, guyana, honduras, croatia, haiti, hungary, indonesia, india,
## ireland, iran, iraq, iceland, italy, jamaica, jordan, kazakhstan, kenya,
## kyrgyzstan, cambodia, kosovo, austria, laos, lebanon, liberia, libya, sri
## lanka, lesotho, lithuania, luxembourg, latvia, morocco
#data setup
Q2INT=Q1INT
Q3FIN=Q2INT%>%group_by(Country)%>%dplyr::summarize(Count=length(City))
#merg to geo-table
locs2=Q3FIN
#add country code for tiles maps
locs2$iso_a3=as.factor(countrycode(Q2FIN$Country,"country.name","iso3c"))
#Map Data
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
folder <- getwd() #set a folder where to download and extract the data
file <- basename(url)
download.file(url, file)
unzip(file, exdir = folder)
#And read it with rgdal library
world <- readOGR(dsn = folder,
layer = "ne_50m_admin_0_countries",
encoding = "UTF-8",
verbose = FALSE)
#setup to merge with "World geomap"
locs2=data.frame(locs2)
#world <- sp::merge(world,locs2, by="iso_a3")
world <- merge(world, locs2,
by.x = "iso_a3",
by.y = "iso_a3",
sort = FALSE)
#world@data$iso_a3
#Colour palette. Check ?colorQuantile and ?RColorBrewer for more options
pal <- colorNumeric(
palette = "Blues",
domain = locs2$Count
)
#Pop up: The info displayed when click on a country
world_popup <- paste(world$admin,
", Buildings:",
world$Count)
#Tiles coming from stamen.com
tiles <- "http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
attribution <- 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Map data by <a href="http://www.naturalearthdata.com/">Natural Earth</a>.'
leaflet(data = world) %>%
addTiles(urlTemplate = tiles,
attribution = attribution) %>%
setView(0, 0, zoom = 2) %>%
addPolygons(fillColor = ~pal(world$Count),
fillOpacity = 1,
color = "#000000",
weight = 1, popup = world_popup) %>%
addLegend("bottomright", pal = pal, values =~Count,
title ="Number of Buildings", opacity = 1)