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?

You can define a
regioninworldmap. And if you decide to go withggplot2::map_data()andggplot2::geom_polygon(), you probably want at least some control over map projection, this is the role ofcoord_sf().Created on 2024-03-25 with reprex v2.1.0