Using Leaflet to Portray South Africa

This project seeks to provide a brief introduction to the top 12 biggest cities in the lovely land of South Africa. Using a very small dataset the aim is to showcase some of the things leaflet can do in a fun and exciting manner.

The Data

I got the data from the geonames site you can find here. This contains a list of the largest cities in South Africa in descending order along with their populations, latitudes and longitudes.

SA_Cities <- data.frame(name = c("Cape Town", "Durban", "Johannesburg", "Soweto","Pretoria", 
                                 "Port Elizabeth", "Pietermaritzburg", "Benoni", "Tembisa",
                                 "East London", "Vereneging", "Bloemfontein"),
                        
                        pop = c(3433441, 3120282, 2026469, 1695047, 1619438,967677, 
                                750845, 605344, 511655, 478676, 474681, 463064),
                        
                        lat = c(-33.926, -29.858, -26.202, -26.268, -25.745, -33.961, 
                                -29.617, -26.188, -25.996, -33.015, -26.673, -29.121),
                        
                        lng = c(18.423, 31.029, 28.044, 27.858, 28.188, 25.615, 
                                30.393, 28.321, 28.227, 27.912, 27.926, 26.214))

For this assignment I want to be truly South African. So the icons needed for the assignment have to be in South African fashion.

springbok_marker <- makeIcon(
  iconUrl <- "https://upload.wikimedia.org/wikipedia/en/8/83/South_Africa_national_rugby_union_team.svg",
  iconWidth = 31*215/230, iconHeight = 31,
  iconAnchorX = 31*215/230/2, iconAnchorY = 16
  
)

We can thus apply the marker and see the first map.

## Assuming "lng" and "lat" are longitude and latitude, respectively

THe next iteration is where the website for each location is displayed.

## Assuming "lng" and "lat" are longitude and latitude, respectively

Much better! However, the cities look a little bunched when they are added together. Let’s use a cluster to help visualise this better.

SA_Cities %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon = springbok_marker, 
             popup = city_sites,
             clusterOptions = markerClusterOptions())
## Assuming "lng" and "lat" are longitude and latitude, respectively