2

See below - I don't understand this behaviour. I can successfully filter by region, but not by subregion. This happens both with and without fortifying the object WorldData1. It also saves like this to file. Any ideas?

library(tidyverse)
WorldData1 <- map_data('world') %>% filter(subregion != '') 

# I am using no characters as filter, but this also happens when putting in a subregion, e.g. 'Alaska'

#> 
#> Attaching package: 'maps'
#> The following object is masked from 'package:purrr':
#> 
#>     map
ggplot() +
  geom_map(data = WorldData1, map = WorldData1,
           aes(x = long, y = lat, map_id = region)) 
#> Warning: Ignoring unknown aesthetics: x, y

Created on 2019-05-12 by the reprex package (v0.2.1)

devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.3 (2019-03-11)
#>  os       macOS Mojave 10.14.3        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         

#>  tidyverse   * 1.2.1   2017-11-14 [1] CRAN (R 3.5.0)
tjebo
  • 21,977
  • 7
  • 58
  • 94

1 Answers1

2

I found the solution - I feel it's worth keeping this question because it points towards an interesting behaviour of filter (see related thread here)

Filtering by subregions removes any NA in subregion, removing the necessary rows for creating the polygons for the countries. Explicitly including NAs in the filter command helps:

library(tidyverse)
WorldData1 <- map_data('world') %>% filter(is.na(subregion)| subregion != 'Alaska') 

ggplot() +
  geom_map(data = WorldData1, map = WorldData1,
           aes(x = long, y = lat, map_id = region))
#> Warning: Ignoring unknown aesthetics: x, y

Created on 2019-05-12 by the reprex package (v0.2.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94