首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggmap绘制多个地图

使用ggmap绘制多个地图
EN

Stack Overflow用户
提问于 2014-02-06 17:53:16
回答 1查看 4.7K关注 0票数 2

我可以用ggmap绘制一张英国地图,其中的一个点如下:

代码语言:javascript
复制
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,这个点就消失了。

代码语言:javascript
复制
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时使点出现

EN

回答 1

Stack Overflow用户

发布于 2014-02-06 18:05:05

multiplot函数不知道该点,因为您只向它传递UK_map对象,该对象不包括该点。要让它绘制点,您需要将geom_point调用添加到UK_map的赋值,如下所示:

代码语言:javascript
复制
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的调用中动态添加点

代码语言:javascript
复制
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)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21599342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档