I’m trying to generate my own heatmap for the prevalence of H5N1 cases by country.
I try the following code:
worldplot(data = Underlag_heatmap, div = 1, ColName = "Cases",CountryName = "iso_a2", CountryNameType = "isoa2",rangeVal=c(0,100))
With the following data:
structure(list(iso_a2 = c("AF", "BD", "KH", "CA", "CL", "CN",
"DJ", "EC", "EG", "IN", "ID", "IQ", "LA", "MM", "NP", "NG", "PK",
"ES", "TH", "TR", "GB", "US", "VN"), Country = c("Azerbaijan",
"Bangladesh", "Cambodia", "Canada", "Chile", "China", "Djibouti",
"Ecuador", "Egypt", "India", "Indonesia", "Iraq", "Lao People's Democratic Republic",
"Myanmar", "Nepal", "Nigeria", "Pakistan", "Spain", "Thailand",
"Turkey", "United Kingdom of Great Britain and Northern Ireland",
"United States of America", "Viet Nam"), ...3 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), Cases = c(8, 8, 62, 1, 1, 55, 1, 1, 359, 1,
200, 3, 3, 1, 1, 1, 3, 2, 25, 12, 5, 1, 128), Fatalities = c(5,
1, 41, 1, 0, 32, 0, 0, 120, 1, 168, 2, 2, 0, 1, 1, 1, 0, 17,
4, 0, 0, 64), `Mortality rate` = c(0.625, 0.125, 0.661290322580645,
1, 0, 0.581818181818182, 0, 0, 0.334261838440111, 1, 0.84, 0.666666666666667,
0.666666666666667, 0, 1, 1, 0.333333333333333, 0, 0.68, 0.333333333333333,
0, 0, 0.5)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-23L))
I get an error message complaining about a missing Mapfiller argument.
Error in `geom_sf()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error:
! object 'MapFiller' not found
I find this confusing since the Mapfiller argument shouldn’t have anything to do with the worldplot function. In the documentation it’s really only mentioned in the function for categorical variables.
It should work after “downgrading” your data from tibble
to data.frame
:
worldplot(data = as.data.frame(Underlag_heatmap), ...)
This error message tipped me off:
Error in countrycode(sourcevar = data[, which(colnames(data) == CountryName)], :
sourcevar must be a character or numeric vector. This error often arises when users
pass a tibble (e.g., from dplyr) instead of a column vector from a data.frame
(i.e., my_tbl[, 2] vs. my_df[, 2] vs. my_tbl[[2]]).
This can also happen when `sourcevar` is entirely composed of `NA`,
which `R` treats as entries of class logical.
worldplot()
is a rather thin wrapper around ggplot()
and rnaturalearth
data. And by installing WorldMapR
you already have all requirements set up.
So unless you are after that specific look, perhaps roll with something like this (works with tibbles too):
library(ggplot2)
library(dplyr)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
rnaturalearthdata::countries50 |>
select(name, iso_a2_eh) |>
left_join(Underlag_heatmap, by = join_by(iso_a2_eh == iso_a2 )) |>
ggplot(aes(fill = Cases)) +
geom_sf(linewidth = .05) +
scale_fill_distiller(palette = "Reds", direction = 1, na.value = "gray95") +
theme_minimal() +
theme(panel.grid = element_blank())
Created on 2024-09-10 with reprex v2.1.1
Here is an example:
worldplot(as.data.frame(Underlag_heatmap),
ColName = "Cases",
CountryName = "iso_a2",
CountryNameType = "isoa2",
rangeVal = c(0,100),
annote = F)