0

I can't get either ggplot's fortify or broom's tidy to include regions, even if I load the maptools library as suggested in another post.

I start by loading a bunch of libraries, including maptools (0.8-41), rgeos (0.3-22), broom (0.4.1) and ggplot2 (2.2.1.9000). Next I grab a map of the world that has a range of different region choices, including the one I'm interested in - ISO_a3 - with the following command.

world <- readOGR(dsn="https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson", layer="OGRGeoJSON")

Next I run map <- broom::tidy(world, region = "iso_a3")

head(map) generates the following. Note that no region column is included.

      long       lat order  hole piece group  id
1 48.93857 11.258447     1 FALSE     1 -99.1 -99
2 48.93848 10.982324     2 FALSE     1 -99.1 -99
3 48.93848 10.714209     3 FALSE     1 -99.1 -99
4 48.93838 10.433252     4 FALSE     1 -99.1 -99
5 48.93828  9.973486     5 FALSE     1 -99.1 -99
6 48.93828  9.807617     6 FALSE     1 -99.1 -99
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
JerryN
  • 2,356
  • 1
  • 15
  • 49
  • 2
    Your github link (in the readOGR) is broken. – Gregor Thomas Jan 31 '17 at 20:12
  • The file can be downloaded at `https://github.com/nvkelso/natural-earth-vector/blob/master/geojson/ne_50m_admin_0_countries.geojson.gz` and the command changed to `world <- readOGR(dsn = "xxx/ne_50m_admin_0_countries.geojson", layer = "OGRGeoJSON")` where xxx is the path to the direction where the file is located. – JerryN Jan 31 '17 at 20:40

1 Answers1

1

Afaik, fortify and tidy do not append a column with the same name. See also the example

if (require("maptools")) {
 sids <- system.file("shapes/sids.shp", package="maptools")
 nc1 <- readShapePoly(sids,
   proj4string = CRS("+proj=longlat +datum=NAD27"))
 nc1_df <- fortify(nc1)
}
head(nc1_df)

You are probably looking for the id column, which contains the iso_a3s:

library(rgdal)
library(ggplot2)
fn <- file.path(tempdir(), "ne_50m_admin_0_countries.geojson.gz")
download.file("https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson.gz", fn)
R.utils::gunzip(fn)
world <- readOGR(dsn = tools::file_path_sans_ext(fn), layer = "OGRGeoJSON")
map <- broom::tidy(world, region="iso_a3")

all(sort(unique(map$id))==sort(levels(world$iso_a3)))
# [1] TRUE

ggplot(map, aes(long, lat, group=group)) + 
  geom_polygon(color="white") + 
  coord_quickmap()
lukeA
  • 53,097
  • 5
  • 97
  • 100