4

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)?

Jay
  • 357
  • 1
  • 3
  • 12
  • There's also https://cran.r-project.org/web/packages/oz/oz.pdf – thelatemail Jun 23 '16 at 00:21
  • Yeah I have tried that but it doesn't have the cities or any population details – Jay Jun 23 '16 at 00:25
  • `world.cities[world.cities$country.etc == "Australia",]` and `map.cities(country = "Australia", minpop=1e6)` – thelatemail Jun 23 '16 at 00:34
  • Thank you. I got the necessary data using world.cities. Does map.cities() plot a map. I did following to mark the cities `Aus_cities= world.cities[world.cities$country.etc == "Australia",]` `class(Aus_cities)` `plot(Aus_cities[Aus_cities@data$pop>1000000,],pch=16,col="red",cex=3,add=TRUE)` – Jay Jun 23 '16 at 01:20

1 Answers1

11

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

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • When I run this code, I get the yellow dots, grey area of the map and legend (not demarcation or outline, no blue area, none of the names on the map) - Any idea why anyone? – chris Apr 20 '20 at 05:36
  • I tried to post my picture regarding the comment above but someone called Samuel Liew thought it appropriate it remove my "answer" lol – chris Apr 20 '20 at 05:39
  • @chris Your "answer" wasn't an answer, it was a question. This is why it was deleted. You need to ask a new question. – SymbolixAU Apr 20 '20 at 09:46