2017-02-26 29 views
0

我想要將ggmap輸出保存爲單個函數的顯示和文件。目前我有2個函數(createMapDisp和createMapDisk),它們將輸出顯示併成功地顯示磁盤(如下所示)。保存ggmap以顯示並從函數中提取文件

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) 

不幸的是這給了我一個

錯誤dev.copy(PNG,文件= 「c.png」,width = 600,height = 600, units =「px」):無法從空設備複製

我可以嵌入一個函數在其他正如我在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(...),我們將不勝感激。

回答

1

結合您的代碼

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) 
+0

R.S.謝謝。你的第一個解決方案是現貨。 – greengrass62

相關問題