简体   繁体   中英

Save ggmap to display and file from a function

I would like to save ggmap output to display and file from a single function. Currently I have 2 functions (createMapDisp and createMapDisk) that will output to display and disk successfully (shown below).

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)

I tried to combine the functions based on my reading here as shown below:

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)

Unfortunately that gives me a

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

I could embed one function in the other as i show in createMapBothB below

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)

However this would not seem to be a good practice. Advice on displaying and saving to disk without re-running ggmap::ggmap(...) would be appreciated.

Combine your code

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)

Alternatively, you just call the 2 functions you already created, although you will be calling get_googlemap twice which is not optimal.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM