如何使用限制(xlim和ylim)放大到R(ggplot / ggmap)中的美国东北地区?

时间:2018-10-12 04:39:44

标签: r ggplot2 ggmap dex-limit

我正在一个项目中,我想使用ggplot或ggmap来“仅”显示美国东北部地区(我们可以使用NYC作为我们的中心)。

我找到了纽约州的x和y,并想为两个轴+/- 10度创建范围,但不知道如何使ggmap仅显示我想要显示的区域。我一直在获取整个地图或仅获得一个点...请帮助...谢谢!

which(usa1$stateName == "new york")
ny.x = usa1$x[which(usa1$stateName=="new york")]
ny.y = usa1$y[which(usa1$stateName=="new york")]
ny.x1 = xlim(ny.x -10, ny.x+10)
ny.y1 = ylim(ny.y -10, ny.y+10)

us.map.zm <- ggplot(usa1, aes(map_id = stateName))
us.map.zm <- us.map.zm + geom_map(map =usa, aes(fill = usa1$Murder),fill="white",color ="black") + ylim(ny.y-10, ny.y+10) + xlim(ny.x-10,ny.x+10)
us.map.zm <- us.map.zm + geom_point(data=usa1,aes(x = usa1$x, y = usa1$y,size=usa1$POP17, color="blue"))
us.map.zm <- us.map.zm + coord_map() + ggtitle("Northeast US Populaton vs Murder rate")
us.map.zm

1 个答案:

答案 0 :(得分:2)

添加经纬度限制

 lat <- c(23.49, 34.5)                
    lon <- c(12.33, 21.36)   

    ### Get a map
    map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 12,
                   maptype = "satellite", source = "google")

    ### When you draw a figure, you limit lon and lat.      
   us.map.zm <- ggmap(map)+
           scale_x_continuous(limits = c(11.33, 11.36), expand = c(0, 0)) +
           scale_y_continuous(limits = c(44.49, 44.5), expand = c(0, 0))

根据您的要求更改并使用它