【问题标题】:Plotting multiple maps with ggmap使用 ggmap 绘制多个地图
【发布时间】:2014-02-06 19:52:41
【问题描述】:

我可以用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's multiplot function,那么该点就会消失。

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时,为什么点会消失?如何让点出现?

【问题讨论】:

    标签: r maps ggmap


    【解决方案1】:

    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)
    

    或者,或者,在对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)
    

    【讨论】:

    • 哎呀,我真的应该发现这一点。
    • ;) 这样的小错误很容易犯。
    • 仓促行事的危险……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2018-08-31
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多