【问题标题】:How to draw a heat map of New York city community based on certain data using R? [closed]如何使用 R 基于某些数据绘制纽约市社区的热图? [关闭]
【发布时间】:2016-04-17 15:47:22
【问题描述】:

对于我目前正在做的一个项目,我必须使用 R 根据某些输入绘制纽约市社区的热图。我搜索了这个主题,发现在 R 中有一个名为“map”的包将允许您很容易地绘制美国各州的热图。但是,我不知道如何为一个城市绘制类似的热图,特别是纽约市?任何建议都非常感谢。

【问题讨论】:

  • 仅供参考,这是我想开始的社区地图。我只是想知道如何使用 R 将其更改为热图? www1.nyc.gov/site/planning/community/community-portal.page
  • 不要使用 cmets 来澄清问题。使用编辑“按钮”打开您的问题进行修改。目前,这不足以支持编程工作。您应该发布数据集、绘图范围和地图文件链接......作为编辑。发布指向通用纽约市网站的链接不属于特定的编程任务。我还没有投票结束。但如果几个小时后这里没有进展,我会的。
  • 社区区域的 shapefile 在这里www1.nyc.gov/site/planning/data-maps/open-data/…

标签: r dictionary plot colors


【解决方案1】:

关于 SO 有很多答案可以帮助您用颜色填充 shapefile 多边形。我同意@42- 你没有表现出任何工作/努力,但这可能有助于其他人希望对纽约社区区数据做类似的事情。所以,为了更大的利益:

library(rgeos)
library(maptools)
library(geojsonio)
library(ggplot2)

# this is the geojson of the NYC community districts
URL <- "http://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nycd/FeatureServer/0/query?where=1=1&outFields=*&outSR=4326&f=geojson"
fil <- "nyc_community_districts.geojson"
if (!file.exists(fil)) download.file(URL, fil)

nyc_districts <- geojson_read(fil, what="sp")

# the @data slot of nyc_districts has 2 fields. BoroCD is the district #
nyc_districts_map <- fortify(nyc_districts, region="BoroCD")

# let's see which id is what
mids <- cbind.data.frame(as.data.frame(gCentroid(nyc_districts, byid=TRUE)), 
                         id=nyc_districts$BoroCD)

gg <- ggplot()
gg <- gg + geom_map(data=nyc_districts_map, map=nyc_districts_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + geom_text(data=mids, aes(x=x, y=y, label=id), size=2)
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
gg

# example choropleth

# we'll just make the map look like the Flash map on the NYC site
colorize <- function(x) {
  switch(substr(x, 1, 1),
         `1`="#86e3ff",
         `2`="#fffe9b",
         `3`="#ffc75f",
         `4`="#e7ceff",
         `5`="#dffd8b")
}

choro <- data.frame(district=nyc_districts@data$BoroCD,
                    fill=sapply(nyc_districts@data$BoroCD, colorize))

gg <- ggplot()
gg <- gg + geom_map(data=nyc_districts_map, map=nyc_districts_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + geom_map(data=choro, map=nyc_districts_map,
                    aes(fill=fill, map_id=district),
                    color="#2b2b2b", size=0.15)
# in a real choropleth you'd make colors map to values
# so i'm definitely leaving you some work to do vs just
# copy/paste for fun and profit
gg <- gg + scale_fill_identity()
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
gg

# better color palette
library(viridis)

# make up some fill data
set.seed(1492)
choro <- data.frame(district=nyc_districts@data$BoroCD,
                    fill=sample(100, nrow(nyc_districts@data)))

gg <- ggplot()
gg <- gg + geom_map(data=nyc_districts_map, map=nyc_districts_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.15, fill=NA)
gg <- gg + geom_map(data=choro, map=nyc_districts_map,
                    aes(fill=fill, map_id=district),
                    color="#2b2b2b", size=0.15)
gg <- gg + scale_fill_viridis(name="Better title\nthan 'fill'")
gg <- gg + coord_map()
gg <- gg + ggthemes::theme_map()
gg <- gg + theme(legend.position=c(0.1,0.5))
gg

【讨论】:

  • 您好,感谢您的帮助。我想出了如何根据我需要的值生成热图。但是,按照您的代码不会让我为情节生成图例?我已经尽我所能,但我仍然无法弄清楚如何让传奇表现出来?你能就如何做到这一点提出一些建议吗?
  • 正确。这不会,但如果您在数据框中有一个数值,请使用 fill=YOURCOLUMN vs fill=fill 并使用 scale_fill_* 函数之一
  • 您好,因为我对 R 很陌生,我尝试使用您提到的功能,但仍然无法获得图例,下面是代码。 gg
  • 更新了一个完整的 choropleth 示例,但这对于 R 来说是一个新的非常雄心勃勃的例子。在 SO 和互联网上有大量示例用于从 shapefile 填充多边形(我写过很多其中)。因此,帖子中的语言很强烈。
  • 非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
相关资源
最近更新 更多