R - 导入 html/json 地图文件以用于热图

R - Import html/json map file to use for heatmap

我正在尝试上传 U.S。 DMA 映射到 R 以创建热图。我找到了 ggplot 并且 choroplehyr packages to be helpful in the mapping part, but don't know how to import my own maps to use. This 是我在网上找到的 DMA 地图,我将如何导入它?

我还假设在上传后,我需要创建自己的 DMA_choropleth 以便根据人口为 DMA 区域着色,对吗?

将tv.json从网站复制到磁盘然后阅读:

df = data.frame(fromJSON("c:\tv.json"))

您将获得一个包含 1050 行的数据框。

您找到的 DMA 映射 ("nielsentopo.json") 是 topojson 格式。要将其导入为可以映射的空间对象,您需要安装 geojsonio 包。这是安装包的注释代码,读取 json 地图,将其转换为数据框,然后绘制。为了给这张地图上的 DMA 区域着色 - 是的,您需要创建自己的等值线 class(有用:https://cran.r-project.org/web/packages/choroplethr/vignettes/h-creating-your-own-maps.html)。

# Package 'geojsonio' installation notes:

# If are a Mac user, first install package gdal using homebrew (open system terminal, type "brew install gdal")
# Then install any required R packages you don't already have:
# install.packages("rgdal", type = "source", configure.args = "--with-gdal-config=/Library/Frameworks/GDAL.framework/Versions/1.11/unix/bin/gdal-config --with-proj-include=/Library/Frameworks/PROJ.framework/unix/include --with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib")
# install.packages("rgeos", type = "source")

# If are a Linux user, first install required libraries (open system terminal, type "sudo apt-get install libgdal1-dev libgdal-dev libgeos-c1v5 libproj-dev libv8-dev")
# Then install any required R packages you don't already have:
# install.packages("rgdal", type = "source")
# install.packages("rgeos", type = "source")

# If you are a Windows user, no preliminary steps are necessary


# Install package geojsonio
install.packages("geojsonio")


# Load required packages
library(geojsonio)
library(rgeos)
library(rgdal)
library(sp)
library(ggmap)
library(ggplot2)
library(jsonlite)


# Read topjson file
myMap <- topojson_read("https://gist.githubusercontent.com/simzou/6459889/raw/fd1cf20e3a9714982bfcedbd5b2117fead27a1bf/nielsentopo.json")

#Convert spatial object myMap into a dataframe so it can be plotted
myMapDF <- fortify(myMap)

# Basic map  plot
ggplot(data = myMapDF, aes(x=long, y=lat, group = group)) +
  geom_polygon(color = "white")

导入后,您的地图如下所示:

您肯定不需要安装geojsonio包。这是一个很棒的包,但使用 rgdal 来完成艰苦的工作。这可以让您获得地图和数据,而无需依赖特殊的 choropleth pkg。

library(sp)
library(rgdal)
library(maptools)
library(rgeos)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(jsonlite)
library(purrr)
library(viridis)
library(scales)

neil <- readOGR("nielsentopo.json", "nielsen_dma", stringsAsFactors=FALSE, 
                verbose=FALSE)
# there are some techincal problems with the polygon that D3 glosses over
neil <- SpatialPolygonsDataFrame(gBuffer(neil, byid=TRUE, width=0),
                                  data=neil@data)
neil_map <- fortify(neil, region="id")

tv <- fromJSON("tv.json", flatten=TRUE)
tv_df <- map_df(tv, as.data.frame, stringsAsFactors=FALSE, .id="id")
colnames(tv_df) <- c("id", "rank", "dma", "tv_homes_count", "pct", "dma_code")
tv_df$pct <- as.numeric(tv_df$pct)/100

gg <- ggplot()
gg <- gg + geom_map(data=neil_map, map=neil_map,
                    aes(x=long, y=lat, map_id=id),
                    color="white", size=0.05, fill=NA)
gg <- gg + geom_map(data=tv_df, map=neil_map,
                    aes(fill=pct, map_id=id),
                    color="white", size=0.05)
gg <- gg + scale_fill_viridis(name="% US", labels=percent)
gg <- gg + coord_proj(paste0("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96",
                             " +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"))
gg <- gg + theme_map()
gg <- gg + theme(legend.position="bottom")
gg <- gg + theme(legend.key.width=unit(2, "cm"))
gg