我正在尝试将美国 DMA 地图上传到 R 中以创建热图。我发现 ggplot 和choroplehyr包在映射部分很有帮助,但不知道如何导入我自己的地图以供使用。这是我在网上找到的DMA图,我要怎么导入呢?
我还假设上传后,我需要创建自己的 DMA_choropleth 以便根据人口为 DMA 区域着色,对吗?
我正在尝试将美国 DMA 地图上传到 R 中以创建热图。我发现 ggplot 和choroplehyr包在映射部分很有帮助,但不知道如何导入我自己的地图以供使用。这是我在网上找到的DMA图,我要怎么导入呢?
我还假设上传后,我需要创建自己的 DMA_choropleth 以便根据人口为 DMA 区域着色,对吗?
您找到的 DMA 映射(“nielsentopo.json”)是 topojson 格式。要将其作为可以映射的空间对象导入,您需要安装 geojsonio 包。这是安装包、读取 json 映射、将其转换为数据框并绘制它的带注释的代码。为了在这张地图上为 DMA 区域着色 - 是的,您需要创建自己的 choropleth 类(有用:https ://cran.r-project.org/web/packages/choroplethr/vignettes/h-creating-你自己的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
将 tv.json 从网站复制到磁盘,然后读取它:
df = data.frame(fromJSON("c:\\tv.json"))
您将获得一个包含 1050 行的数据框。