【问题标题】:How do I save a tiff background image in R with exactly the same dimensions as my input tiff如何在 R 中保存与输入 tiff 完全相同尺寸的 tiff 背景图像
【发布时间】: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 输入运行我的代码,问题是一样的

【问题讨论】:

    标签: r image ggplot2 geo


    【解决方案1】:

    不确定您面临的问题是什么,回复太长而无法在评论中发布,因此是一个答案。

    我正在使用magick::image_read() 来读取背景图片。由于我不知道您要绘制什么,因此我使用 mtcars 作为示例数据集来检查它是否在循环中工作 - 要绘制的变量和 scale_y_continuous 都取决于列绘制可能与您的用例相似。

    lapply(c('data.table', 'ggplot2', 'grid', 'png', 'magick'), 
           library, character.only = T)
    
    img <- image_read('https://i.stack.imgur.com/IwjyT.png')
    dat <- as.data.table(mtcars)
    plot_list <- lapply(colnames(dat)[1:5], function(z){
      plt <- ggplot(dat, aes_string(x = 'wt', y = z)) + 
        theme(legend.position = 'none') + 
        annotation_custom(grob = rasterGrob(image = img, 
                                            width = unit(1, 'npc'), 
                                            height = unit(1, 'npc')), 
                          xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) + 
        geom_point() + geom_line() + 
        scale_x_continuous(limits = dat[, range(wt)]) + 
        scale_y_continuous(limits = dat[, range(get(z))])
    
      ggsave(filename = sprintf('%s vs Wt.png', z), plot = plt, device = 'png')
    
      return(plt)
    
    })
    

    我发现绘图没有问题 - 通过在本地保存绘图来确认。图像大小唯一存在差异的情况是 y 轴的刻度线有更多数字(例如,绘制 cyl 与绘制 hp 时)。

    我也用theme_void 对此进行了测试 - 它只删除了 x、y 轴,对绘图本身没有任何影响。

    【讨论】:

    • 我的投票没有显示“因为我是“新手”。不过谢谢
    【解决方案2】:

    更新:

    我将“保存”功能从“tiff + print”保存更改为“ggsave”保存。

    @gautam 提供的答案给了我方向

    另外我发现了这个enter link description here

    代码现在看起来像这样

    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))
    ggsave("world_packground_out.tiff", width=10.4, height=5.89, dpi=600, units="in", compression = "lzw")
    #print(myplt)
    #dev.off()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 2014-11-24
      • 2018-03-30
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多