Our youtube channel

Our youtube channel has lots of videos on data visualisation in r.

Visit our youtube channel https://www.youtube.com/c/TechAnswers88

Watch this tutorial in action on youtube

https://youtu.be/xXXqTvv5g3M

Easiest way to plot map of Australia and create a Choropleth maps using any data

What is Choropleth maps

In simple words these are geographical maps which are filled with different colours based on any characteristic which you want to show. eg. World map showing the population of each country and using a graded shade of colour.

Packages used

library(ozmaps)   
library(sf)
library(ggplot2)

Create our first map

Just using the ozmap() command you can draw a map of Australia.

ozmap()

Extract map data from ozmap

This is an important step. You are getting the map geometry data and putting it into a new object called sf_oz.

sf_oz <- ozmap("states")

Create a map using ggplot

Let us use ggplot to plot a map

pl <- ggplot(data = sf_oz) + geom_sf()
pl             

Create data

We will use this fictitious data to define the number of cases for each state.


Cases <- c(
  3820
  ,13035
  ,1088
  ,456
  ,670
  ,229
  ,33
  ,113
  ,0)

Cases
FALSE [1]  3820 13035  1088   456   670   229    33   113     0

Add the data to the map

Now add the Cases data created above into a new column in the map dataset.

sf_oz$Cases <- Cases
sf_oz
FALSE Simple feature collection with 9 features and 2 fields
FALSE Geometry type: MULTIPOLYGON
FALSE Dimension:     XY
FALSE Bounding box:  xmin: 105.5507 ymin: -43.63203 xmax: 167.9969 ymax: -9.229287
FALSE Geodetic CRS:  GDA94
FALSE # A tibble: 9 x 3
FALSE   NAME                                                            geometry Cases
FALSE * <chr>                                                 <MULTIPOLYGON [°]> <dbl>
FALSE 1 New South Wales    (((150.7016 -35.12286, 150.6611 -35.11782, 150.6373 ~  3820
FALSE 2 Victoria           (((146.6196 -38.70196, 146.6721 -38.70259, 146.6744 ~ 13035
FALSE 3 Queensland         (((148.8473 -20.3457, 148.8722 -20.37575, 148.8515 -~  1088
FALSE 4 South Australia    (((137.3481 -34.48242, 137.3749 -34.46885, 137.3805 ~   456
FALSE 5 Western Australia  (((126.3868 -14.01168, 126.3625 -13.98264, 126.3765 ~   670
FALSE 6 Tasmania           (((147.8397 -40.29844, 147.8902 -40.30258, 147.8812 ~   229
FALSE 7 Northern Territory (((136.3669 -13.84237, 136.3339 -13.83922, 136.3532 ~    33
FALSE 8 Australian Capita~ (((149.2317 -35.222, 149.2346 -35.24047, 149.2716 -3~   113
FALSE 9 Other Territories  (((167.9333 -29.05421, 167.9188 -29.0344, 167.9313 -~     0

Plot the Choropleth

We are now ready to plot our chorpleth map of Australia. Simply use the fill command using the aes(fill = Cases). This will fill each state with a different fill colour/shade.

This is what a basic Choropleth map is all about.


pl <- ggplot(data = sf_oz, aes(fill = Cases)) + geom_sf()
pl

pl <- ggplot(data = sf_oz, aes(fill = Cases)) + geom_sf()
pl <- pl + scale_fill_gradient(low ="green", high = "red")
pl <- pl + labs(title ="Map of Australia")
pl <- pl + labs(subtitle ="Showing states")
pl <- pl + theme_void()
pl

Show each state separately

Using the theme_wrap command, we can create multiple charts. Each state shown in a new chart as shown below. We have added a border around each chart so it good.

pl <- ggplot(data = sf_oz, aes(fill = Cases)) + geom_sf()
pl <- pl + scale_fill_gradient(low ="green", high = "red")
pl <- pl + labs(title ="Map of Australia")
pl <- pl + labs(subtitle ="Showing states")
pl <- pl + theme_void()

pl <- pl + theme(panel.border = element_rect(color = "#1b98e0",
                                    fill = NA,
                                    size = 1))
pl <- pl + facet_wrap( ~ NAME)
pl

Watch this tutorial in action on youtube

https://youtu.be/xXXqTvv5g3M