用ggmap绘制多个地图 [英] Plotting multiple maps with ggmap

查看:71
本文介绍了用ggmap绘制多个地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用 ggmap 用这样的点绘制英国地图:

I can plot a UK map with ggmap with a point like this:

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 函数,该点消失了.

However, if I try to use Winston Chang's multiplot function, the point disappears.

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 时,点消失了,如何显示点?

Why is the point disappearing and how can I get point to appear when using multiplot?

推荐答案

multiplot 函数不知道该点,因为您仅将其传递给了 UK_map 对象,其中不包括该点.要绘制点,您需要将 geom_point 调用添加到 UK_map 的分配中,例如:

The multiplot function doesn't know about the point, since you only pass it your UK_map object, which does not include the point. To have it plot the point, you would need to add the geom_point call to assignment of UK_map, like so:

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 的调用中即时添加点:

Or, alternatively, add the point on the fly within the call to 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绘制多个地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆