保存 ggmap 以从函数显示和归档

Save ggmap to display and file from a function

我想保存 ggmap 输出以通过单个函数显示和归档。目前我有 2 个函数(createMapDisp 和 createMapDisk)可以成功输出到显示器和磁盘(如下所示)。

sites <- data.frame(Organization = c("OrgA","OrgB"),
                    Longitude    = c(-91.08,-91.1),
                    Latitude     = c(32, 32.1), 
                    stringsAsFactors = FALSE)

createMapDisp <- function(sites) {
  map <- ggmap::get_googlemap(center = c(lon = mean(sites$Longitude), 
                                         lat = mean(sites$Latitude)), 
                              zoom=11, size = c(640, 640), 
                              style = c(feature = "terrain", element = "labels", visibility = "off"))

  ggmap::ggmap(map, legend = "right" ) +
    ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                        data = sites, alpha = 1.0, size = 2, shape=19)
}
createMapDisk <- function(sites) {
  map <- ggmap::get_googlemap(center = c(lon = mean(sites$Longitude), 
                                         lat = mean(sites$Latitude)), 
                              zoom=11, size = c(640, 640), 
                              style = c(feature = "terrain", element = "labels", visibility = "off"))

  p <- ggmap::ggmap(map, legend = "right" ) +
    ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                        data = sites, alpha = 1.0, size = 2, shape=19)

  ggplot2::ggsave(filename="a.png", plot=p, width=6, height=6, units = "in")
}

createMapDisp(sites)
createMapDisk(sites)

我尝试根据我的阅读here组合功能,如下所示:

createMapBothA <- function(sites) { 
  map <- ggmap::get_googlemap(center = c(lon = mean(sites$Longitude), 
                                         lat = mean(sites$Latitude)), 
                              zoom=11, size = c(640, 640), 
                              style = c(feature = "terrain", element = "labels", visibility = "off"))

  ggmap::ggmap(map, legend = "right" ) +
    ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                        data = sites, alpha = 1.0, size = 2, shape=19)

  dev.copy(png,file='c.png', width = 600, height = 600, units="px")
  dev.off()  
}

createMapBothA(sites)

不幸的是,这给了我

Error in dev.copy(png, file = "c.png", width = 600, height = 600, units = "px") : cannot copy from the null device

我可以将一个函数嵌入另一个函数,如下面的 createMapBothB 所示

createMapBothB <- function(sites) {

  map2disk <- function(sites, map) {
    p <- ggmap::ggmap(map, legend = "right" ) +
      ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                          data = sites, alpha = 1.0, size = 2, shape=19)
    ggplot2::ggsave(filename="a.png", plot=p, width=6, height=6, units = "in")
  }

  map <- ggmap::get_googlemap(center = c(lon = mean(sites$Longitude), 
                                         lat = mean(sites$Latitude)), 
                              zoom=11, size = c(640, 640), 
                              style = c(feature = "terrain", element = "labels", visibility = "off"))

  map2disk(sites,map)

  ggmap::ggmap(map, legend = "right" ) +
    ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                        data = sites, alpha = 1.0, size = 2, shape=19)
}

createMapBothB(sites)

然而,这似乎不是一个好的做法。关于在不重新 运行 ggmap::ggmap(...) 的情况下显示和保存到磁盘的建议将不胜感激。

合并您的代码

createMapBoth <- function(sites) {

    map <- ggmap::get_googlemap(center = c(lon = mean(sites$Longitude), 
                                       lat = mean(sites$Latitude)), 
                            zoom=11, size = c(640, 640), 
                            style = c(feature = "terrain", element = "labels", visibility = "off"))

    map2disp <- ggmap::ggmap(map, legend = "right" ) +
      ggplot2::geom_point(ggplot2::aes(x = Longitude, y = Latitude, color = Organization),
                      data = sites, alpha = 1.0, size = 2, shape=19)

    map2disk <-ggplot2::ggsave(filename="c.png", plot=map2disp, width=6, height=6, units = "in")

    print(map2disp)
    print(map2disk)   
}

createMapBoth(sites)

或者,您只需调用已经创建的 2 个函数,尽管您将调用 get_googlemap 两次,这不是最佳选择。

createMapBoth <- function(sites) {
  createMapDisp(sites)
  createMapDisk(sites)
}
createMapBoth(sites)