Leaflet: us-states.geojson

Created on 8 Mar 2018  路  12Comments  路  Source: rstudio/leaflet

Hi,

I was trying to follow the choropleth tutorial and couldn't find the us-states,geojson file in your website. I found a file with a similar name in https://github.com/shawnbot/topogram/raw/master/data/us-states.geojson but the content was not the same and it didn't contain the population density variable. Can you give me directions where I can find the file or convert such a file to geojson format?

Thanks
Edmund

Most helpful comment

Hi @DocOfi ,

I found the .json file in another GitHub repo. Please try running the following:

# load necessary packages
library( geojsonio )

# transfrom .json file into a spatial polygons data frame
states <- 
    geojson_read( 
        x = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
        , what = "sp"
    )

# check the class of the object
class( states )
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

# end of script #

All 12 comments

Hi @DocOfi ,

I found the .json file in another GitHub repo. Please try running the following:

# load necessary packages
library( geojsonio )

# transfrom .json file into a spatial polygons data frame
states <- 
    geojson_read( 
        x = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
        , what = "sp"
    )

# check the class of the object
class( states )
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

# end of script #

Hi cenuno,
Thanks for the quick reply and for the marvelous job of finding the data. Tried everything to find it, even tried 'accio us-states.geojson' but I think you need Harry's wand for that to work. I'm working on a personal project http://www.rpubs.com/DocOfi/367052 You're comments will be highly appreciated.

BTW, the code for the tutorial works in the console but when I use it to create an html file in RStudio I get an error on this chunk of code:
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)

m %>% addPolygons(
fillColor = ~pal(density),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7)

The error : Error in cut.default(x , binsTouse, label = FALSE, include.lowest = TRUE, : ' x' must be numeric calls:...resolveFormula -> eval -> eval -> pal -> cut -> cut.default

@DocOfi your project seems very cool! I especially liked the movement on one of your markers in the leaflet map. I would love to see how you did that.

Would you mind republishing your output by modifying the code_folding option to in your YAML header? That way people like me can click on the Code button on your HTML document and see the code; while other people can simply read the HTML document without having to look at your code.

HAhaha. I just finished the course Building Data Visualization Tools and YAML was not really discussed. I've been studying R for 2 years now but practically still a newbie.

Here's my YAML
title: "Plotting Natural Disasters"
author: "Edmund Julian Ofilada"
date: "February 26, 2018"
output:
html_document:
toc: true
toc_depth: 2
toc_float: true
theme: spacelab
highlight: tango

Just let me know how i can make that work. BTW I'll be asking the datascience github.io page if they'll like to include a link to the github repository where i'll keep the Rmd files. I'll be putting all the codes at the last part for all the datascience students of Coursera to see. I just need to finish a few things about the references. Thanks.

I just republished the file and you'll find all the codes at the last part except for the geom_hurricane function to make the hurricane icon. That's part of the requirement of the course. Thanks again for your help and comment.

@DocOfi here is how you incorporate the code_folding option into your YAML header inside the .rmd file.

This will eliminate the need for your to create a separate R Code section at the bottom of your HTML output.

Also, feel free to 'Close' this issue now that it has been resolved.

---
title: "Plotting Natural Disasters"
author: "Edmund Julian Ofilada"
date: "February 26, 2018"
output:
   html_document:
      code_folding: hide
      toc: true
      toc_depth: 2
      toc_float: true
      theme: spacelab
      highlight: tango
---

Many thanks!

Sorry, the data was here:
https://github.com/rstudio/leaflet/tree/gh-pages/json

I'll add a note to the docs.

Actually @schloerke can you add the note to the docs when you get back?

Thanks jcheng. Since I got the data for us-states.json, I was too embarrassed to ask for the rest. Many thanks again. One last bit, would you mind telling me where to find all the arguments for the YAML. Seems like I never paid too much attention to that. Thanks.

Hi @DocOfi ,

I found the .json file in another GitHub repo. Please try running the following:

# load necessary packages
library( geojsonio )

# transfrom .json file into a spatial polygons data frame
states <- 
    geojson_read( 
        x = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
        , what = "sp"
    )

# check the class of the object
class( states )
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

# end of script #

Thank you very much

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loerasg picture loerasg  路  6Comments

sl4269 picture sl4269  路  4Comments

BastienFR picture BastienFR  路  5Comments

BilboBaagins picture BilboBaagins  路  3Comments

hadley picture hadley  路  5Comments