【问题标题】:R - Import html/json map file to use for heatmapR - 导入 html/json 地图文件以用于热图
【发布时间】:2016-02-18 22:19:25
【问题描述】:

我正在尝试将美国 DMA 地图上传到 R 以创建热图。我发现 ggplot 和 choroplehyr 包对映射部分很有帮助,但不知道如何导入我自己的地图以供使用。 This 是我在网上找到的 DMA 地图,请问如何导入呢?

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

【问题讨论】:

    标签: r ggplot2 choropleth


    【解决方案1】:

    您肯定不需要需要安装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
    

    【讨论】:

    • 由于某种原因,fortify() 不起作用。它不“喜欢” region = 'id' 参数。你能帮忙吗?
    • 我返回了这个错误:几何组件中的点太少在点 -82.061259930000006 26.499350110000002环自相交在点或附近 -89.1404651 29.135150639999999环自相交在点或附近 -76051 33.867667130000001SpP is invalid Invalid objects found;考虑在 rgeos::gUnaryUnion(spgeom = SpP, id = IDs) 中使用 set_RGEOS_CheckValidity(2L)Error: TopologyException: Input geom 0 is invalid: Ring Self-intersection at or near point -78.518767870000005 33.867667130000001 at -78.51876787000005 03008 03008 03008
    【解决方案2】:

    您找到的 DMA 映射(“nielsentopo.json”)是 topojson 格式。要将其作为可映射的空间对象导入,您需要安装 geojsonio 包。这是安装包、读取 json 映射、将其转换为数据框并绘制它的带注释的代码。为了给这张地图上的 DMA 区域着色 - 是的,您需要创建自己的 choropleth 类(有用: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")
    

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-06
        • 2019-11-07
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 2020-09-29
        • 2022-11-20
        相关资源
        最近更新 更多