2

I'm trying the R cartography package. Had to work to find a US state shapefile that would like to work with the cartography stuff - many seemed too big, etc.. I seemed to get everything going well, but the state of Colorado misplots.

library(cartography)
library(sf)
library(RColorBrewer)
library(maps)
library(ggplot2)

rm(list = ls())


# USA shape file
states <- st_as_sf(map("state", plot = F, fill = TRUE))

#seems to plot correctly here
#ggplot(states) + geom_sf(aes(fill = ID))

usa <- st_transform(states, 
                    CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96"))

# still seems to plot fine
#ggplot(usa) + geom_sf(aes(fill = ID))

usa <- st_buffer(usa, dist=0)

datamap <- usa

datamap$randoVar <- sample(1:3, length(datamap$ID), replace = T)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)

plot(st_geometry(usa), col = "white", border = "black", bg = "lightblue1")

typoLayer(
  x = datamap_pencil,
  var="randoVar",
  col = c("aquamarine4", "yellow3","#3c5cb0"),
  lwd = .7,
  legend.values.order = 1:3,
  legend.pos = "bottomleft",
  legend.title.txt = "",
  add = TRUE
)

labelLayer(x = datamap, txt = "ID", 
           cex = 0.9, halo = TRUE, r = 0.15)

I first noticed because when I tried to merge in a data file and do a fill with that feature, colorado came up as "No Data". Likewise, the code above seems to indicate the state gemometry or ID is off. I don't know enough GIS to understand why. I did have to change the CRS projection so that I could buffer the map file (getPencilLayer kept throwing a self-intercection error, which seems to be common with R mapping).

enter image description here

Any ideas on what to do?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
James Holland
  • 1,102
  • 10
  • 17
  • 2
    For some reason the `st_buffer` is turning Colorado into a very small triangle. You can see it with `ggplot(usa[usa$ID=='colorado',]) + geom_sf()`. I don't know why this is happening, but this isolates the problem a bit. – Kent Johnson Feb 07 '20 at 17:50

2 Answers2

1

Well, I ended up fixing by using a shapefile from the US Census

https://www2.census.gov/geo/tiger/TIGER2017/STATE/

states <- st_read("#mypath#/tl_2017_us_state/tl_2017_us_state.shp")
states <- states[!(states$NAME %in% 
                     c("Commonwealth of the Northern Mariana Islands", "United States Virgin Islands", 
                       "Puerto Rico", "American Samoa", "Hawaii", "Guam", "Alaska")
), ]

usa <- st_transform(states, 
                     CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96"))

usa <- st_buffer(usa, dist=0)

Not sure how to adjust for getting the geodate from map("state"...) but this worked for me.

Used these buffer and size settings (later after merging in data)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)
James Holland
  • 1,102
  • 10
  • 17
0

Already answered here: https://gis.stackexchange.com/a/351910/142200

The problem is that the initial map object is non-valid

library(cartography)
library(sf)
library(RColorBrewer)
library(maps)
library(ggplot2)

rm(list = ls())
# USA shape file
states <- st_as_sf(map("state", plot = F, fill = TRUE))

usa <- st_transform(states,
                    "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96")


datamap <- usa
# Check validity----
st_is_valid(datamap)
#>  [1]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [13]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [37]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
#> [49]  TRUE

#Make valid
library(lwgeom)
datamap<-st_make_valid(datamap)
st_is_valid(datamap)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [46] TRUE TRUE TRUE TRUE

# Start cartography

datamap$randoVar <- sample(1:3, length(datamap$ID), replace = T)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)
plot(st_geometry(usa), col = "white", border = "black", bg = "lightblue1")
typoLayer(
  x = datamap_pencil,
  var="randoVar",
  col = c("aquamarine4", "yellow3","#3c5cb0"),
  lwd = .7,
  legend.values.order = 1:3,
  legend.pos = "bottomleft",
  legend.title.txt = "",
  add = TRUE
)

enter image description here

Created on 2020-02-25 by the reprex package (v0.3.0)

dieghernan
  • 2,690
  • 8
  • 16