I am creating a geographic map of data using ggplto2 and it's geom_sf capability. Ultimately, I'm will create an animation of data using gganimate. As such, I'm hoping to keep the placement of the logo within the functions of the ggplot2 library.
I'd like to place a logo in the lower-left corner of the map. When I use the annotation_raster function it's not obvious what the " location (in data coordinates) giving the horizontal location of raster" [see https://ggplot2.tidyverse.org/reference/annotation_raster.html] should be in this case. In the examples provided the coordinates are x & y on an xy plot.
using xmin=-Inf and ymin=-Inf anchors the image in the lower-left corner. What values of ymax and xmax should be used to place the image with a small size that does not impinge on the map?
The example code is below.
library(urbnmapr)
library(ggplot2)
library(dplyr)
library(magick)
# Read the logo
logo <- image_read(path='C:\\your\\local\\directory\\myawesomelogo.PNG')
# Obtain the polygon data for states
states_sf <- get_urbn_map("states", sf = TRUE)
# Remove AK and HI
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
# create random data
dataplot <- tibble(xdata=runif(49,-1,1))
# add FIPS data
dataplot <- bind_cols(dataplot, tibble(state_fips = states_sf$state_fips))
# Merge the polygon data into the data data
dataplot <- left_join(states_sf, dataplot, by = "state_fips")
# Plot the random data
dataplot %>%
ggplot() +
geom_sf(mapping = aes(fill = xdata), size=0.5, color=NA) +
coord_sf(datum = NA) +
labs(fill = "Random",
title = 'Map of Data with a Logo') +
scale_fill_gradient2(low='navyblue', high='red', limits=c(-1,1)) + # Add the logo
annotation_raster(logo, ymin = -Inf, ymax = 1, xmin = -Inf, xmax = 1) +
theme_bw() +
theme(
panel.border = element_blank(),
plot.title = element_text(hjust = 0.5))
I'd like the logo to be much smaller and tucked away in the lower-left corner of the image. Changing the size of the image seems to have no effect.
Thank you for your suggestions!