Well, see here an adaptation:
- First you need to get a
sf
(a map ) of the US states. I use here USAboundaries
but you can use whaterver you prefer.
- Note that I needed to fake your data. Use your own.
- Use
rowwise()
and switch()
to add a column to your data with the url of the png
- On the loop, for each brand: select the corresponding state, create the image overlay and add the layer to the plot.
See here a reproducible example:
library(USAboundaries)
#> The USAboundariesData package needs to be installed.
#> Please try installing the package using the following command:
#> install.packages("USAboundariesData", repos = "https://ropensci.r-universe.dev", type = "source")
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(tidyverse)
library(rasterpic)
library(tidyterra)
# Part 1: The map
states <- USAboundaries::states_contemporary_lores %>%
select(State = state_abbr) %>%
# Filter AK and HW
filter(!(State %in% c("AK", "HI", "PR"))) %>%
st_transform("ESRI:102003")
states
#> Simple feature collection with 49 features and 1 field
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -2356114 ymin: -1338125 xmax: 2258154 ymax: 1558935
#> Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
#> First 10 features:
#> State geometry
#> 1 CA MULTIPOLYGON (((-2066285 -2...
#> 2 WI MULTIPOLYGON (((708320.1 91...
#> 3 ID MULTIPOLYGON (((-1673882 95...
#> 4 MN MULTIPOLYGON (((-91052.17 1...
#> 5 IA MULTIPOLYGON (((-50588.83 5...
#> 6 MO MULTIPOLYGON (((19670.04 34...
#> 7 MD MULTIPOLYGON (((1722285 240...
#> 8 OR MULTIPOLYGON (((-2285910 94...
#> 9 MI MULTIPOLYGON (((882371.5 99...
#> 10 MT MULTIPOLYGON (((-1474367 14...
# Base map
plot <- ggplot(states) +
geom_sf(fill = "grey90") +
theme_minimal() +
theme(panel.background = element_rect(fill = "lightblue"))
plot

# Part 2: your data (I have to fake it)
# Use here your own data
# Assign 3 random brands
brands <- data.frame(State = states$State)
set.seed(1234)
brands$Brand <- sample(c("Nestle", "Mars", "Coca Cola"), nrow(brands), replace = TRUE)
# Part 3: find your pngs
logos <- brands %>%
rowwise() %>%
mutate(png = switch(Brand,
"Nestle" = "https://1000marcas.net/wp-content/uploads/2020/01/Logo-Nestle.png",
"Mars" = "https://1000marcas.net/wp-content/uploads/2020/02/Logo-Mars.png",
"Coca Cola" = "https://w7.pngwing.com/pngs/873/613/png-transparent-world-of-coca-cola-fizzy-drinks-diet-coke-coca-cola-text-logo-cola.png",
"null"
))
# Now loop
for (i in seq_len(nrow(logos))) {
logo <- logos[i, ]
shape <- states[states$State == logo$State, ]
img <- rasterpic_img(shape, logo$png, mask = TRUE)
plot <- plot + geom_spatraster_rgb(data = img)
}
plot

Created on 2022-06-03 by the reprex package (v2.0.1)