gpt4 book ai didi

r - 使用点、分面和每个分面适当缩放来创建 ggmap?

转载 作者:行者123 更新时间:2023-12-04 09:34:51 25 4
gpt4 key购买 nike

我想在谷歌的灰色 map 上绘制各个城市的数据点。由于这些城市彼此之间有一些距离,我想我会使用多面图。

创建 map 很容易;请参阅下面的图像和代码。但是,每个方面都显示相同的区域 - 在本例中为大伦敦 - 结果其他城市的点没有显示。

理想情况下,我希望每个方面都显示每个城市并叠加相关点。因此,构面“Cardiff”将显示 Cardiff 及其数据点的缩放 map ,“Birmingham”将显示伯明翰及其点等。我尝试更改各种参数,例如 zoomcenter但我没有成功。

如何在每个方面显示不同的城市和相关点?

enter image description here

require(ggmap)
require(reshape)

# create fake data
sites <- data.frame(site = 1:6,
name = c(
"Royal Albert Hall",
"Tower of London",
"Wales Millenium Centre",
"Cardiff Bay Barrage",
"Birmingham Bullring",
"Birmingham New Street Station"
),
coords = c(
"51.501076,-0.177265",
"51.508075,-0.07605",
"51.465211,-3.163208",
"51.44609,-3.166652",
"52.477644,-1.894158",
"52.477487,-1.898836"),
subzone = rep(c('London','Cardiff','Birmingham'), each = 2)
)

# use function from reshape to split/add column
sites = transform(sites,
new = colsplit(coords, split = ",", names = c('lat', 'lon')))
names(sites) <- c(names(sites)[1:4], 'lat','lon')


ggmap(get_googlemap(center = "London", # omitting this doesn't help
scale = 2,
zoom = 11, # fiddling with zoom doesn't work
color = 'bw',
maptype = 'roadmap',
extent = 'panel',
format = "png8",
filename = "facet_map_test",
)) +
facet_wrap(~ subzone, ncol = 1) +
geom_point(data = sites,
aes(x = lon, y = lat),
fill = "red",
size = 3,
colour = "black",
shape = 21,
alpha = 1) +
theme(legend.position = "none") +
theme()

最佳答案

使用 gridExtra打包可能是最好的方法。编码:

# getting the maps
londonmap <- get_map(location = c(lon = -0.1266575, lat = 51.504575), zoom = 12)
cardiffmap <- get_map(location = c(lon = -3.16493, lat = 51.45565), zoom = 13)
birminghammap <- get_map(location = c(lon = -1.896497, lat = 52.477565), zoom = 14)

# plotting the maps
p1 <- ggmap(londonmap) +
geom_point(data = sites[sites$subzone=="London",],
aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3),
shape = 21) +
ggtitle("London") +
theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines"))

p2 <- ggmap(cardiffmap) +
geom_point(data = sites[sites$subzone=="Cardiff",],
aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3),
shape = 21) +
ggtitle("Cardiff") +
theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines"))

p3 <- ggmap(birminghammap) +
geom_point(data = sites[sites$subzone=="Birmingham",],
aes(x = lon, y = lat, fill = "red", alpha = 0.8, size = 3),
shape = 21) +
ggtitle("Birmingham") +
theme(axis.title = element_blank(), legend.position = "none", plot.margin = unit(c(0,0,0,0), "lines"))

# grouping the plots together in one plot
grid.arrange(p1,p2,p3,ncol=1)

结果:

enter image description here

关于r - 使用点、分面和每个分面适当缩放来创建 ggmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21575279/

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