gpt4 book ai didi

r - 使用 ggmap 绘制多个 map

转载 作者:行者123 更新时间:2023-12-04 10:14:42 25 4
gpt4 key购买 nike

我可以用 ggmap 绘制英国 map ,其中一个点如下:

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)

enter image description here

但是,如果我尝试使用 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)

enter image description here

为什么点消失了,我怎样才能在使用 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

关于r - 使用 ggmap 绘制多个 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21599342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com