gpt4 book ai didi

r - 金县 map 未正确显示边界

转载 作者:行者123 更新时间:2023-12-04 15:39:58 25 4
gpt4 key购买 nike

您好,我正在尝试绘制华盛顿州金县的 map ,因为我需要放置在 map 中的数据点很少。以下是我使用的。

long <- c(47.5112,47.7210   ,47.3684,44)
lat <- c(-122.257, -122.319, -122.031,-120)
price <- c(287655,456355,662500,234563)

House <- data.frame(long, lat, price)

states <- map_data("state")
# west_coast <- states %>%
# filter(region %in% c("washington"))

wa_df <- states %>%
filter(region == "washington", subregion == 'king')



counties <- map_data("county")
wa_county <- counties %>%
filter(region == "washington", subregion == 'king')

wa_base <-
ggplot(data = wa_df,
mapping = aes(x = long, y = lat, group = group)) +
geom_point(
data = House,
mapping = aes(x = long, y = lat),
color = "red",
inherit.aes = FALSE
) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
#geom_point(data = House, mapping = aes(x = long, y = lat), color = "red")

wa_base + theme_nothing() +
geom_polygon(data = wa_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA) # get the state border back on top

以下是我收到的 map 。我不觉得 map 看起来不错。请帮忙

enter image description here

最佳答案

我是 R 的新手,这是我的第一 react ,但我可能已经找到了解决这个问题的方法。从 map 包下载的边界不准确。您可以在此处下载正确的边界数据作为 shapefile:https://gis-kingcounty.opendata.arcgis.com/datasets/kingcounty::king-county-political-boundary-no-waterbodies-kingco-area/explore?location=47.477749%2C-121.920728%2C9.83

使用:

library(ggmap)
library(mapdata)
library(rgdal)
library(tidyverse)

之后,将 shapefile 读入 R 并使用 map_data() 将其转换为数据框:

kc_bound <- readOGR(dsn="~/Desktop/King_County",layer="King_County_Political_Boundary_(no_waterbodies)___kingco_area")
kc_bound_df <- map_data(kc_bound)

假设您还处理来自 kaggle (https://www.kaggle.com/harlfoxem/housesalesprediction) 的房价预测数据集,您可以运行以下代码来获取周围有金县边界的房屋图(df 是您的数据框):

ggplot() + geom_point(df,mapping=aes(x=long,y=lat),color="red") + geom_polygon(kc_bound_df,mapping=aes(x=long,y=lat),fill=NA, color="black")

结果见此图:Houses in King County plotted with county boundary

希望这对您有所帮助,让我知道您的想法!

关于r - 金县 map 未正确显示边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146170/

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