1

I am trying to plot some data from the Malaria Atlas Project. Therefore, I looked in the vignette and used an example for getting started:

if (!require("malariaAtlas")) install.packages("malariaAtlas")
    
shape <- getShp(ISO = "MDG", admin_level = "admin1")
raster <- getRaster(dataset_id = "Explorer__2020_Global_PfPR", shp = shape, year = 2013)

p <- autoplot(object = raster,
              legend_title = "",
              plot_titles = FALSE, 
              fill_colour_palette = "PuBu")

resulting in the following graph:

test pic 1

and the warning message:

In geom_spatraster(data = object, use_coltab = use_coltab, ...): Ignoring unknown parameters: legend_title, plot_titles, and fill_colour_palette

The alternative plotting version autoplot_MAPraster enables some customization:

p <- autoplot_MAPraster(object = raster, 
                        legend_title = "", 
                        plot_titles = FALSE, 
                        fill_colour_palette = "PuBu")

test pic 2

For my application, I need to customize the graphs (legend color and title, plot shapes over the raster, custom title, ...). Therefore, I checked the source code and modified the autoplot.MAPshp code without problems, but I don't know how to get ggplot2::geom_raster(data = object[object$raster_name == rastername,], ggplot2::aes_string(x="x", y="y", fill = "z")) working for the autoplot.MAPraster code. In particular, object[object$raster_name == rastername,], where object = raster, I get an error:

Error in h(simpleError(msg, call)) :
Error in evaluating the argument 'i' in selecting a method for function '[': [subset] invalid name(s)

My goal would be a (for me) working version of the source code for autoplot.MAPraster, that can be modified.

EDIT

For better differentiation of the different parts of the map, it would be useful to transfer the previously calculated country boundaries (shapes) on top of the existing raster.

shape <- getShp(ISO = "MDG", admin_level = "admin1")
autoplot(shape)

test pic 3

From the autoplot.MAPraster code something like

p <- p + geom_polygon(data = shape, aes_string(x = "long", y = "lat", group = "group"), alpha = 0, colour = "grey40")

could do the job. For me, this results in error massage:

Error in geom_polygon(): ! Problem while computing aesthetics. i Error occurred in the 3rd layer. Caused by error: ! Object 'group' not found

3 Answers 3

2

When working with geospatial data, it is much easier to treat them as such. This is especially true in the case of ggplot2, which handles geospatial data really well. For instance, "shape" in your example is an sf object, so plotting it using geom_sf() is a more robust approach. Similarly, "raster" is a SpatRaster so you can use tidyterra::geom_spatraster().

This repex uses some of the example code from the malariaAtlas GitHub. If you want different fill colours, there are numerous scale_fill_*() options. I have included two options (one is commented out, you can switch between the two for comparison). I set the colour of "shape" to "black" for illustrative purposes as "grey40" did not show clearly, but you can employ any ggplot2 aesthetics, colours, and fills to suit your needs.

library(malariaAtlas)
library(tidyterra)

shape <- getShp(ISO = "MDG", admin_level = "admin1")

raster <- getRaster(dataset_id = "Explorer__2020_Global_PfPR", shp = shape, year = 2013)

ggplot() +
  geom_spatraster(data = raster) +
  geom_sf(data = shape, colour = "black", fill = NA) +
  ggtitle("Raw PfPR Survey points +\nModelled PfPR 2-10 in Madagascar in 2013") +
  scale_fill_distiller(name = "PfPR", 
                       palette = "RdYlBu",
                       na.value = "transparent") +
  # scale_fill_gradient(low = "lightblue", high = "darkblue", 
  #                     na.value = "transparent") +
  coord_sf(xlim = c(42.5, 51)) +
  theme(plot.title = element_text(size = 10))

result

1

You can use ggtitle() for the main title, labs() for the legend title, scale_fill_gradient() for coloring the map and theme_classic() for the plot's theme.

p + ggtitle("Some Title") +
  labs(fill="Legend Title") +
  scale_fill_gradient(low = "lightgreen", high = "darkgreen") +
  theme_classic()

Output: enter image description here

3
  • Is there a way to add the shape on top of the raster?
    – Excelsior
    Commented May 20 at 12:16
  • Would you explain more please? Example maybe. Commented May 20 at 12:27
  • I editted the question accordingly
    – Excelsior
    Commented May 20 at 12:49
0

Customization may be easier with base plot. To get started:

library(terra)
plot(raster)
lines(shape, col="gray")

Not the answer you're looking for? Browse other questions tagged or ask your own question.