【发布时间】:2020-07-17 14:35:04
【问题描述】:
我正在使用 ggplot 在 R 中添加背景图像。我在一个迭代过程中这样做,构建多个具有相同背景的幻灯片。
我有一个 tiff 格式的输入世界地图,我将其读入 ggmap 进程,然后将其写为 tiff 文件。
world map with urban areas highlighted
我有两个问题需要帮助: 1. 与输入文件相比,保存的世界地图图片略微向右下方写入。仔细看才能看到这一点。 2. 保存的文件丢失分辨率。
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()
如果您在迭代的基础上运行此程序,使用保存的文件作为下一次迭代的输入,您将看到世界图片在您生成的每个输出中向下和向右滑动,并且分辨率变得越来越粗糙。
关于将图像保持在完全相同的位置有什么建议吗?以及如何保持我的决心?
注意:上传的图片以png格式保存(我认为!)。如果你用 png 输入运行我的代码,问题是一样的
【问题讨论】: