tract_choropleth in Choroplethr v3.6.0 for R - r

I'm trying to replicate the maps shown in this short example (Choroplethr v3.6.0 is now on CRAN) for California, but I'm having trouble with the tract_choropleth function for areas that are not in NY. I get the error message below. I'm able to import the underlying tract data for all areas, but I'm having trouble mapping the non-NY ones. Thanks in advance.
# Load all packages (DONE)
# Install api key (DONE)
# Get Tract-Level Demographics
ca_stats = get_tract_demographics("california")
# Create choropleth map with the function tract_choropleth
ca_stats$value = ca_stats$per_capita_income
tract_choropleth(ca_stats, "california", title = "2013 Per Capita Income\nCensus Tracts", legend="Dollars")
Error message returned:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding,
use_iconv = use_iconv, : Cannot open layer In addition: Warning
message: In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
error 1 in extracting from zip file

I'm the author of the tigris package that choroplethr uses to get Census tract boundaries. That error shows up in the event of a corrupted download from the Census Bureau website. Ari's solution will work; you can also try:
library(tigris)
options(tigris_refresh = TRUE)
This will direct tigris to re-download the shapefile from the Census Bureau website, and allow you to still use caching. Once it is fixed, set options(tigris_refresh = FALSE) to control future re-downloads.

Thank you for using choroplethr.
I have never gotten this error myself, but it's possible that it relates to a caching issue with the tigris package. I say that because it looks very similar to this issue.
Here is the code which solved that user's problem:
library(tigris)
options(tigris_use_cache = FALSE)
That code causes tigris to not use its cache.
Please type that code into your R session and try the choroplethr code again.

Related

Error in CRS(slot(obj, "projargs")) : NA using adehabitatHR

I am trying to get home range data from a set of points using the package adehabitathr. Previously I was able to get all of the information in the form of SpatialPolygonsDataFrames. I wanted to export them as shapefiles, and attempted to use code I found on here
dir.create("esrishapes") #create new directory
writeOGR(obj = homerangeAll, dsn = "esrishapes", layer = "homerangeAll", driver = "ESRI
Shapefile")
But I was getting a projection error about not using PROJ6, adehabitathr uses PROJ4 so I attempted to download previous versions of sp and rgdal instead
require(devtools)
install_version("sp",version = "1.4-2")
install_version("rgdal",version = "1.5-8")
This didn't work even though the packages did install. Now I can't use any aspect of adehabitathr as it returns the error
Error in CRS(slot(obj, "projargs")) : NA
I don't have a strong understanding of coordinate systems so I have no idea where to begin fixing this.

Cannot load a shapefile in R "Error: no features found"

I'm trying to read the shapefile that you can download with this url.
I have a code similar to the next one to download automatically the files:
library("raster")}
url<-"http://www6.gipuzkoa.eus/CATASTRO/Planos/ZIP-A098.zip"
downloader::download(url, dest=paste0(getwd(),"/","my_file.zip"), mode="wb",quiet=T)
zipped_shape_names<-c("098_HELBIDE_SHP/ATRIBUTOAK-A098.cpg","098_HELBIDE_SHP/ATRIBUTOAK-A098.dbf","098_HELBIDE_SHP/ATRIBUTOAK-A098.shp","098_HELBIDE_SHP/ATRIBUTOAK-A098.shx")
unzip("my_file.zip", files=zipped_shape_names)
my_shape<-raster::shapefile("098_HELBIDE_SHP/ATRIBUTOAK-A098.shp")
But what I obtain is the following error:
Error in rgdal::readOGR(dirname(x), fn, stringsAsFactors = stringsAsFactors, :
no features found
In addition: Warning messages:
1: In .local(x, ...) : .prj file is missing
2: In ogrFIDs(dsn = dsn, layer = layer) : no features found
You can access to the original web page by this link and pressing "Descargar planos"
I don't have this problem with others areas, just with this and another one, but I don't know what is happening with this specific area.
Any help will be appreciated.
The error message is quite clear. no features found means that your shapefile is empty. You can check this in multiple ways.
One is to add your shapefile in Q-Gis or any other GIS-software tool. In the case of Q-GIS your shapefile will pop-up in the layers pane, but you won't see any features.
It is also possible to check the .dbf file in R:
library(foreign)
read.dbf("098_HELBIDE_SHP/ATRIBUTOAK-A098.dbf")
The .dbf should contain as many rows as there are features. In your case None.
Simpler code would be
library(raster)
url<-"http://www6.gipuzkoa.eus/CATASTRO/Planos/ZIP-A098.zip"
download.file(url, dest="my_file.zip")
unzip("my_file.zip")
s <- raster::shapefile("098_HELBIDE_SHP/ATRIBUTOAK-A098.shp")
Clearly that file is empty. However, it works for this file:
s <-raster::shapefile("098_LANDALUR_SHP/LANDALUR-PARTZELAK-A098.shp")

acs package in R: Cannot download dataset, error message is inscrutable

I am trying to use the acs package in R to download Census data for a basic map, but I am unable to download the data and I'm receiving a confusing error message.
My code is as follows:
#Including all packages here in case this is somehow the issue
install.packages(c("choroplethr", "choroplethrMaps", "tidycensus", "tigris", "leaflet", "acs", "sf"))
library(choroplethr)
library(choroplethrMaps)
library(tidycensus)
library(tigris)
library(leaflet)
library(acs)
library(sf)
library(tidyverse)
api.key.install("my_api_key")
SD_geo <- geo.make(state="CA", county = 73, tract = "*", block.group = "*")
median_income <- acs.fetch(endyear = 2015, span = 5, geography = SD_geo, table.number = "B19013", col.names="pretty")
Everything appears to work until the final command, when I receive the following error message:
trying URL 'http://web.mit.edu/eglenn/www/acs/acs-variables/acs_5yr_2015_var.xml.gz'
Content type 'application/xml' length 735879 bytes (718 KB)
downloaded 718 KB
Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In (function (endyear, span = 5, dataset = "acs", keyword, table.name, :
XML variable lookup tables for this request
seem to be missing from ' https://api.census.gov/data/2015/acs5/variables.xml ';
temporarily downloading and using archived copies instead;
since this is *much* slower, recommend running
acs.tables.install()
This is puzzling to me because 1) it appears like something is in fact being downloaded at first? and 2) 'Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed' makes no sense to me. It doesn't align with any of the arguments in the function.
I have tried:
Downloading the tables using acs.tables.install() as recommended in the second half of the error message. Doesn't help.
Changing the endyear and span to be sure that I'm falling within the years of data supported by the API. I seem to be, according to the API documentation. Have also used the package default arguments with no luck.
Using 'variable =' and the code for the variable as found in the official API documentation. This returns only the two lines with the mysterious "Error in if..." message.
Removing colnames = "pretty"
I'm going to just download the datafile as a CSV and read it into R for now, but I'd like to be able to perform this function from the script for future maps. Any information on what's going on here would be appreciated. I am running R version 3.3.2. Also, I'm new to using this package and the API. But I'm following the documentation and can't find evidence that I'm doing anything wrong.
Tutorial I am working off of:
http://zevross.com/blog/2015/10/14/manipulating-and-mapping-us-census-data-in-r-using-the-acs-tigris-and-leaflet-packages-3/#get-the-tabular-data-acs
And documentation of the acs package: http://eglenn.scripts.mit.edu/citystate/wp-content/uploads/2013/02/wpid-working_with_acs_R2.pdf
To follow up on Brandon's comment, version 2.1.1 of the package is now on CRAN, which should resolve this issue.
Your code runs for me. My guess would be that the Census API was temporarily down.
As you loaded tidycensus and you'd like to do some mapping, you might also consider the following code:
library(tidycensus)
census_api_key("your key here") # use `install = TRUE` to install the key
options(tigris_use_cache = TRUE) # optional - to cache the Census shapefile
median_income <- get_acs(geography = "block group",
variables = "B19013_001",
state = "CA", county = "San Diego",
geometry = TRUE)
This will get you the data you need, along with feature geometry for mapping, as a tidy data frame.
I emailed Ezra Haber Glenn, the author of the package, about this as I was having the same issue. I received a response within 30 minutes and it was after midnight, which I thought was amazing. Long story short, the acs package version 2.1.0 is configured to work with the changes the Census Bureau is making to their API later this summer, and it is currently presenting some problems windows users in the mean time. Ezra is going to be releasing an update with a fix, but in the mean time I reverted back to version 2.0 and it works fine. I'm sure there are a few ways to do this, but I installed the devtools package and ran:
require(devtools)
install_version("acs", version = "2.0", repos = "http://cran.us.r-project.org")
Hope this helps anyone else having a similar issue.

Errors working through plotting shapefiles in the ggplot wiki

I'm trying to learn how to plot shapefiles with ggplot2 and I found a wiki at:
https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
I downloaded and extracted the zip file provided into new directory on my desktop ( http://www.epa.gov/nheerl/arm/documents/design_doc/ecoregion_design.zip ).
I started reading through the article, going line by line but I'm running into a problem with fortify.
require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")
> my_dsn <- "directory here"
>
> utah = readOGR(dsn=my_dsn, layer="eco_l3_ut")
OGR data source with driver: ESRI Shapefile
Source: "directory here", layer: "eco_l3_ut"
with 10 features and 7 fields
Feature type: wkbPolygon with 2 dimensions
> utah#data$id = rownames(utah#data)
> utah.points = fortify(utah, region="id")
Error: isTRUE(gpclibPermitStatus()) is not TRUE
I looked at the ?fortify help page and get the jist of how it should be used. I think its being used correctly. I also found another question with the same error on SO,
Getting error while using fortify function in R (ggplot)
but I can't figure out how to apply what is being said for my solution. I might help if I had access to dropbox right now but I don't.
The short answer is to install the rgeos package and include it at the top.
A longer answer can be found here (and elsewhere): https://stat.ethz.ch/pipermail/r-sig-geo/2012-July/015685.html

Shapefiles in R

I'm relatively new to using Shapefiles in R. I want to create visualizations of country and district level data for India for which I'm using maptools in R. For the shapefiles, I got the data from gadm.org which includes state/district/subdistrict level data for India.
However, I'm trying to load the file in R and it's just not happening.This is my code :
library(maptools)
dist <- readShapePoly("IND_adm2.shp")
Here IND_adm2.shp is the district level shape file for India.
R is unable to read the file, giving me this error:
"Error in getinfo.shape(filen) : Error opening SHP file"
I'm not really sure what the problem is.I have R 3.0.1, have the files in the appropriate directory but something's just not right. I'm also new to making maps in R so it's been a bit annoying. Any ideas on how to fix this?
You could use the package rgdal
library(rgdal)
library(sp)
shp <- readOGR("path/to/shpfiles", "IND_adm2")
plot(shp)

Resources