January 20, 2018

2016 Census of Population

Appendix (Code)

library(htmltab)
library(plotly)
## Read Table from wikipedia (data was accessed on 
## 19/01/2018, wiki information might change anytime)

table1 <- htmltab(doc = "https://en.wikipedia.org/wiki/List_of_
                  municipalities_in_British_Columbia
                  #Municipalities", 1)

Subset data frame

BC <- subset(table1, 
             select = c("Name", 
                        "Status", 
                        "2016 Census of Population >> 
                        Population (2016)", 
                        "2016 Census of Population >> 
                        Land area (km²)",
                        "2016 Census of Population >> 
                        density"))
BC <- rename(BC, Population="2016 Census of Population >> 
             Population (2016)")
BC <- rename(BC, Area="2016 Census of Population >> 
             Land area (km²)")
BC <- rename(BC, Density="2016 Census of Population >> 
             Population density")

Cleaning Data

BC<- BC[-c(162:172),]
BC$Density <- substr(BC$Density, 1, nchar(BC$Density)-3)
BC$Population <- as.numeric(gsub(",", "", BC$Population))
BC$Area <- as.numeric(gsub(",", "", BC$Area))
BC$Density <- as.numeric(gsub(",", "", BC$Density))
BC <- BC[-which(is.na(BC$Population)),]

Plot (Annotation)

## Annotations

a <- list(
  x = log(100),
  y = log(100),
  text = "Circle size shows <br> Population Density",
  showarrow = FALSE,
  font = list(color = '#264E86',
              family = 'sans serif',
              size = 16)
)

Plot

## Plot

plot_ly(BC, x = ~Area, y = ~Population, text = ~Name, 
        size = ~Density, color = ~as.factor(Status), 
        type = "scatter", mode = "markers") %>% 
  layout(annotations = a,
         title = "Cities in British Columbia 
         (2016 Census of Population and Area)",
         xaxis = list(type = "log", title = "Area (km<sup>2</sup>)"), 
         yaxis = list(type = "log"))