Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Australian cities - R spatial

Tags:

r

maps

spatial

sp

I want to draw a map of Australia and represent each city as a dot. Then highlight the cities with a high population (>1M)

library(sp)
library(maps)
data(canada.cities)
head(canada.cities)

I have checked the sp package where this can be done for Canada and some other countries. But Australia details are not there. Is there a special way to get the data for a country we like (name of cities, long, lat, pop)?

like image 654
Jay Avatar asked Dec 25 '22 04:12

Jay


1 Answers

Now you have the data using world.cities, you can plot them a few ways

library(maps)
df <- world.cities[world.cities$country.etc == "Australia",]

Basic plot of points

plot(df[, c("long", "lat")])

on a ggmap

library(ggmap)

myMap <- get_map(location = "Australia", zoom = 4)

ggmap(myMap) +
geom_point(data = df[, c("long","lat", "pop")], aes(x=long, y = lat, colour = pop > 1000000))

enter image description here

On a leaflet map

library(leaflet)

## define a palette for hte colour
pal <- colorNumeric(palette = "YlOrRd",
                    domain = df$pop)

leaflet(data = df) %>%
    addTiles() %>%
    addCircleMarkers(lat = ~lat, lng = ~long, popup = ~name, 
                     color = ~pal(pop), stroke = FALSE, fillOpacity = 0.6) %>%
    addLegend(position = "bottomleft", pal = pal, values = ~pop)

enter image description here

like image 85
SymbolixAU Avatar answered Jan 12 '23 14:01

SymbolixAU