簡體   English   中英

使用ggmap繪制多個地圖

[英]Plotting multiple maps with ggmap

我可以使用ggmap繪制一張英國地圖, ggmap包含以下內容:

library(ggmap)
UK_map <- get_map(location = c(-2.65, 53.7), zoom = 5, maptype = "hybrid")
UK_map <- ggmap(ggmap=UK_map, extent = "device", legend = "right")
UK_map + geom_point(data = data.frame(x = -1.81, y = 55.655), aes(x, y), size  = 5)

在此輸入圖像描述

但是,如果我嘗試使用Winston Chang的multiplot功能 ,該點將消失。

multiplot <- function(..., plotlist=NULL, cols) {
    require(grid)

    # Make a list from the ... arguments and plotlist
    plots <- c(list(...), plotlist)

    numPlots = length(plots)

    # Make the panel
    plotCols = cols                          # Number of columns of plots
    plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
    vplayout <- function(x, y)
        viewport(layout.pos.row = x, layout.pos.col = y)

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
        curRow = ceiling(i/plotCols)
        curCol = (i-1) %% plotCols + 1
        print(plots[[i]], vp = vplayout(curRow, curCol ))
    }

}

multiplot(UK_map, UK_map, cols = 2)

在此輸入圖像描述

為什么點消失,我怎么能得到點使用時出現multiplot

multiplot功能不知道這一點,因為你只把它傳遞您的UK_map對象,這不包括點。 把它繪制點,你需要在添加geom_point呼叫分配UK_map ,就像這樣:

UK_map_with_point <- UK_map + 
  geom_point(data = data.frame(x = -1.81, y = 55.655), aes(x, y), size  = 5)

multiplot(UK_map_with_point, UK_map, cols = 2)

ggmap multiplot

或者,或者,在多multiplot調用中動態添加點:

multiplot(UK_map + geom_point(data = data.frame(x = -1.81, y = 55.655), 
                              aes(x, y), size  = 5), 
          UK_map + geom_point(data = data.frame(x = -2.81, y = 56.655), 
                              aes(x, y), size  = 5), cols = 2)

ggmap multiplot2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM