gpt4 book ai didi

r - 在 ggplot/ggmap 中省略一些边框

转载 作者:行者123 更新时间:2023-12-04 06:21:01 24 4
gpt4 key购买 nike

我对 R 很陌生,我只是在玩一个项目来绘制 2010 年到 2020 年阿拉巴马县的预计人口增长。这是我的代码:

dat <- read.table("C:/Users/rasmus/Documents/countyinfo.txt", sep="\t", header=TRUE)

library(ggplot2)
library(maps)
library(ggmap)

mdat <- map_data('county')
str(mdat)
al1 = get_map(location = c(lon = -86.304474, lat = 32.362563),
zoom = 7, maptype = 'roadmap')
al1MAP = ggmap(al1) +
geom_point(data=dat,inherit.aes = FALSE,
aes(y=Lat, x=Lon, map_id=County, size=Growth), col="red") +
borders("state", colour="red", alpha=0.8) +
borders("county", colour="blue", alpha=0.5)
al1MAP

现在,我有两个问题。

1) 国家边界似乎在做一些奇怪的事情。这是关闭县覆盖的屏幕截图:

enter image description here

2)鉴于这仅与阿拉巴马州有关,我想关闭州边界以外的边界,但我不知道该怎么做。我的猜测是尝试使用 xlimylim ,但我不知道如何将这些限制到阿拉巴马州边界多边形。

最佳答案

似乎与功能borders()因为某些状态的坐标是连接在一起的。

要解决这个问题,您可以存储 state使用 map_data() 将边框作为单独的数据框然后添加 state边框使用 geom_path()到你的 map 。在 geom_path()添加 group=region以确保点仅根据一个区域连接。

要仅显示阿拉巴马县的边界,您应该添加参数 region="alabama"功能 borders() .

al1 = get_map(location = c(lon = -86.304474, lat = 32.362563), 
zoom = 6, maptype = 'roadmap')
mdat <- map_data('state')
ggmap(al1) +
geom_path(data=mdat,aes(x=long,y=lat,group=region),colour="red",alpha=0.8)+
borders("county", colour="blue", alpha=0.5,region="alabama")

enter image description here

关于r - 在 ggplot/ggmap 中省略一些边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15677225/

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