How to plot heatmap with WorldMapR (or similar package)

I’m trying to generate my own heatmap for the prevalence of H5N1 cases by country.

I try the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>worldplot(data = Underlag_heatmap, div = 1, ColName = "Cases",CountryName = "iso_a2", CountryNameType = "isoa2",rangeVal=c(0,100))
</code>
<code>worldplot(data = Underlag_heatmap, div = 1, ColName = "Cases",CountryName = "iso_a2", CountryNameType = "isoa2",rangeVal=c(0,100)) </code>
worldplot(data = Underlag_heatmap, div = 1, ColName = "Cases",CountryName = "iso_a2", CountryNameType = "isoa2",rangeVal=c(0,100))

With the following data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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))
</code>
<code> 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)) </code>
    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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error in `geom_sf()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error:
! object 'MapFiller' not found
</code>
<code>Error in `geom_sf()`: ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error: ! object 'MapFiller' not found </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
worldplot(data = as.data.frame(Underlag_heatmap), ...)
</code>
<code> worldplot(data = as.data.frame(Underlag_heatmap), ...) </code>

    worldplot(data = as.data.frame(Underlag_heatmap), ...)

This error message tipped me off:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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.
</code>
<code>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. </code>
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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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())
</code>
<code>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()) </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>worldplot(as.data.frame(Underlag_heatmap),
ColName = "Cases",
CountryName = "iso_a2",
CountryNameType = "isoa2",
rangeVal = c(0,100),
annote = F)
</code>
<code>worldplot(as.data.frame(Underlag_heatmap), ColName = "Cases", CountryName = "iso_a2", CountryNameType = "isoa2", rangeVal = c(0,100), annote = F) </code>
worldplot(as.data.frame(Underlag_heatmap),
          ColName = "Cases",
          CountryName = "iso_a2",
          CountryNameType = "isoa2",
          rangeVal = c(0,100),
          annote = F)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật