I am adding a background image in R using ggplot. I am doing this in an iterative process, building up multiple slides with the same background.
I have an input worldmap in tiff format, I read it into a ggmap process and then write it out as a tiff file.
world map with urban areas highlighted
I have two problems which I would like some help with: 1. The saved world map picture is written slightly down and to the right in comparison to the input file. Look closely to see this. 2. The saved file loses resolution.
world_background <-tiff::readTIFF("world_background_in.tiff", native = TRUE)
myplt <- ggplot() +
theme_void() + theme(legend.position="none") +
annotation_custom(rasterGrob(world_background,
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
scale_x_continuous(limits = c(-180, 180)) +
scale_y_continuous(limits = c(-79.99688, 79.99825))
tiff("world_packground_out.tiff", width=1024, height=580, res=300, compression = "lzw")
print(myplt)
dev.off()
If you run this on an iterative basis, using the saved file as input for the next iteration, you will see that the world pic slides down and right for each output you produce, and the resolution becomes increasingly grainy.
Any suggestion on to keep the image in exactly the same location? And how do I maintain my resolution?
Note: The uploaded image is saved in png format (I think!). If you run my code with png input, the problem is the same