【问题标题】:King County map not displaying the boundaries correctly金县地图未正确显示边界
【发布时间】:2021-08-01 15:46:50
【问题描述】:

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

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

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

【问题讨论】:

  • House 数据在哪里?此外,当我运行 wa_df &lt;- states %&gt;% filter(region == "washington", subregion == 'king') 时,我得到一个空数据框!
  • 您好...请立即查看
  • 顺便说一句,它应该是price &lt;- as.Date(c(287655,456355,662500), origin = "1970-01-01")。我确实在情节中得到了 1 个红点和 1 个黑点,所以似乎正在工作......
  • 对不起维塔利。我犯了一个错误。请立即检查数据框

标签: r


【解决方案1】:

我是 R 新手,这是我的第一反应,但我可能已经找到了解决此问题的方法。从地图包下载的边界不准确。您可以在此处下载正确的边界数据作为 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2022-01-17
    • 2014-07-15
    • 2017-08-27
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多