R语言 创建点,面,每个面适当缩放的ggmap?

pftdvrlh  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(112)

我想在google的灰色Map上绘制不同城市的数据点,因为这些城市之间有一定的距离,我想我会使用一个小平面图。
创建Map非常简单;请看下面的图片和代码。2然而,每个面显示相同的区域-在这个例子中是大伦敦-结果是其他城市的点没有显示。
理想情况下,我希望每个面都显示每个城市,并覆盖相关点。因此,面“卡迪夫”将显示Cardiff及其数据点的缩放Map,“Birmingham”将显示Birmingham及其数据点,以此类推。我尝试更改各种参数,如zoomcenter,但没有成功。
我如何展示一个不同的城市和每个方面的相关点?

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()
ni65a41a

ni65a41a1#

使用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)

结果是:

相关问题