How to create a HeatMap of Australia

52 views Asked by At

I have found the following source which documents how to create a heatmap of the world map.

# load required packages
library(ggplot2)
library(mapdata)

# create example data with coordinates and values
lon <- c(9.481544, 2.352222, -74.005973, 139.650312)
lat <- c(51.312801, 48.856613, 40.712776, 35.676191)
value <- c(12, 15, 19, 30)
foo1 <- data.frame(lon, lat, value)

# get world map data
world_map <- map_data("world")

# create heat map using ggplot2
ggplot() +
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), fill = "white", color = "grey") +
  geom_point(data = foo1, aes(x = lon, y = lat, fill = value), size = 5, shape = 21) +
  scale_fill_gradient(low = "white", high = "red") +
  labs(x = "Longitude", y = "Latitude", title = "Example Heat Map")

However when I want to replicate this with my own data, because my lat/long data is only within Australia, I would like to only see Australia appear on the map, as using the above code makes it hard to see the heat map as Australia only makes up a small proportion of the map. The "mapdata" package doesn't seem to have an option for Australian only based on the documentation here.

Does anyone know how I can modify the sample code to achieve what I am after please? Alternatively is there another way I can create a heat map of Australia?

2

There are 2 answers

0
margusl On

You can define a region in world map. And if you decide to go with ggplot2::map_data() and ggplot2::geom_polygon(), you probably want at least some control over map projection, this is the role of coord_sf().

library(ggplot2)

# define a region to get Australia
map_aus <- map_data("world", region = "Australia")

# generate example data considering map coordinates
set.seed(42)
example_data <- data.frame(
  lon = rnorm(10, mean = 135, sd = 8 ),
  lat = rnorm(10, mean = -25, sd = 3 ),
  value = runif(10,1,100)
)

# create heat map using ggplot2
p <- 
  ggplot() +
  geom_polygon(data = map_aus, aes(x = long, y = lat, group = group), fill = "white", color = "grey") +
  geom_point(data = example_data, aes(x = lon, y = lat, fill = value), size = 5, shape = 21) +
  scale_fill_gradient(low = "white", high = "red") +
  labs(x = "Longitude", y = "Latitude", title = "Example Heat Map")

p

p + coord_sf(crs = 3857) + ggtitle("WGS84 / Pseudo-Mercator")

p + coord_sf(crs = 3107) + ggtitle("GDA94 / SA Lambert")

Created on 2024-03-25 with reprex v2.1.0

0
dieghernan On

My advice is to migrate completely to spatial formats in R (using sf package). If you need the shape of a country you can use several packages, like rnaturalearth of giscoR, check the section Specific geospatial data sources of interest on CRAN Task View: Analysis of Spatial Data.

In this reprex I use maps to get a data frame of coordinates and transform it to an sf object, and giscoR to get the shape of Australia:

library(ggplot2)
library(maps)

# Use spatial formats
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(giscoR)

foo1 <- subset(world.cities, country.etc == "Australia")
head(foo1)
#>               name country.etc     pop    lat   long capital
#> 387       Adelaide   Australia 1076969 -34.93 138.60       0
#> 856         Albany   Australia   24338 -35.02 117.88       0
#> 889         Albury   Australia  109273 -36.06 146.92       0
#> 1017 Alice Springs   Australia   26178 -23.70 133.87       0
#> 1205     Alyangula   Australia    1286 -13.83 136.42       0
#> 1781        Ararat   Australia    6111 -37.28 142.93       0

# Make foo1 spatial
foo1_sf <- st_as_sf(foo1, coords = c("long", "lat"), crs = 4326)

head(foo1_sf)
#> Simple feature collection with 6 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 117.88 ymin: -37.28 xmax: 146.92 ymax: -13.83
#> Geodetic CRS:  WGS 84
#>               name country.etc     pop capital              geometry
#> 387       Adelaide   Australia 1076969       0  POINT (138.6 -34.93)
#> 856         Albany   Australia   24338       0 POINT (117.88 -35.02)
#> 889         Albury   Australia  109273       0 POINT (146.92 -36.06)
#> 1017 Alice Springs   Australia   26178       0  POINT (133.87 -23.7)
#> 1205     Alyangula   Australia    1286       0 POINT (136.42 -13.83)
#> 1781        Ararat   Australia    6111       0 POINT (142.93 -37.28)

# Shape of Australia
aus <- gisco_get_countries(country = "Australia", resolution = 1)


# create heat map using ggplot2
ggplot() +
  geom_sf(data = aus, fill = "white", color = "grey") +
  geom_sf(data = foo1_sf, aes(fill = pop), size = 5, shape = 21) +
  scale_fill_gradient(
    low = "white", high = "red", transform = "log",
    labels = scales::label_comma()
  ) +
  labs(x = "Longitude", y = "Latitude", title = "Example Heat Map")

Created on 2024-03-25 with reprex v2.1.0