Unable to use custom facet_geo from geofacet package

195 views Asked by At

I am having difficulties using a custom grid in the geofacet package.

Here is a reprodutible example:

The custom grid (done using https://hafen.github.io/grid-designer/) has a structure similar to the package´s examples:

library(tidyverse)
library(geofacet)

mygrid <- data.frame(
  name = c("PERUS", "TUCURUVI", "JAÇANÃ", "BRASILÂNDIA", "JARAGUÁ", "LAUZANE PAULISTA", "ERMELINO MATARAZZO", "CANGAÍBA", "VILA JACUÍ", "VILA SABRINA", "ITAIM PAULISTA", "JARDIM HELENA", "SÃO MIGUEL PAULISTA", "PIRITUBA", "NOSSA SENHORA DO Ó", "SANTANA", "CASA VERDE", "PENHA DE FRANÇA", "PONTE RASA", "VILA MARIA", "LAPA", "PERDIZES", "SANTA IFIGÊNIA", "ITAQUERA", "VILA MATILDE", "GUAIANASES", "CONJUNTO JOSÉ BONIFÁCIO", "MOÓCA", "BELA VISTA", "PINHEIROS", "JARDIM PAULISTA", "PARQUE DO CARMO", "VILA FORMOSA", "SAPOPEMBA", "IPIRANGA", "VILA MARIANA", "TATUAPÉ", "RIO PEQUENO", "BUTANTÃ", "VILA PRUDENTE", "SÃO MATEUS", "CIDADE TIRADENTES", "SAÚDE", "INDIANÓPOLIS", "JARDIM SÃO LUÍS", "CAMPO LIMPO", "TEOTÔNIO VILELA", "CURSINO", "JABAQUARA", "CAPÃO REDONDO", "SANTO AMARO", "CIDADE ADEMAR", "PEDREIRA", "PIRAPORINHA", "CAPELA DO SOCORRO", "GRAJAÚ", "VALO VELHO", "PARELHEIROS"),
  col = c(2, 4, 5, 3, 2, 4, 7, 6, 8, 5, 10, 9, 8, 1, 2, 4, 3, 6, 7, 5, 2, 3, 4, 7, 6, 9, 8, 5, 4, 2, 3, 7, 6, 8, 5, 4, 7, 2, 3, 6, 8, 9, 5, 4, 3, 2, 7, 6, 5, 2, 3, 4, 5, 2, 3, 4, 2, 3),
  row = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10),
  stringsAsFactors = FALSE
)
mygrid$code <- mygrid$name
geofacet::grid_preview(mygrid)

enter image description here

My data, showing here using dput()

d <- structure(list(name = c("BELA VISTA", "BELA VISTA", "PERDIZES", 
"PERDIZES", "SANTA IFIGÊNIA", "SANTA IFIGÊNIA", "MOÓCA", "MOÓCA", 
"JARDIM PAULISTA", "JARDIM PAULISTA", "VILA MARIANA", "VILA MARIANA", 
"VALO VELHO", "VALO VELHO", "SANTO AMARO", "SANTO AMARO", "SÃO MIGUEL PAULISTA", 
"SÃO MIGUEL PAULISTA", "ITAQUERA", "ITAQUERA", "SANTANA", "SANTANA", 
"LAPA", "LAPA", "PINHEIROS", "PINHEIROS", "PENHA DE FRANÇA", 
"PENHA DE FRANÇA", "TATUAPÉ", "TATUAPÉ", "VILA MARIA", "VILA MARIA", 
"CASA VERDE", "CASA VERDE", "TUCURUVI", "TUCURUVI", "VILA PRUDENTE", 
"VILA PRUDENTE", "INDIANÓPOLIS", "INDIANÓPOLIS", "SAÚDE", 
"SAÚDE", "IPIRANGA", "IPIRANGA", "CAPELA DO SOCORRO", "CAPELA DO SOCORRO", 
"JABAQUARA", "JABAQUARA", "PIRITUBA", "PIRITUBA", "ERMELINO MATARAZZO", 
"ERMELINO MATARAZZO", "NOSSA SENHORA DO Ó", "NOSSA SENHORA DO Ó", 
"CAMPO LIMPO", "CAMPO LIMPO", "BUTANTÃ", "BUTANTÃ", "VILA MATILDE", 
"VILA MATILDE", "VILA FORMOSA", "VILA FORMOSA", "JAÇANÃ", "JAÇANÃ", 
"SAPOPEMBA", "SAPOPEMBA", "CIDADE ADEMAR", "CIDADE ADEMAR", "ITAIM PAULISTA", 
"ITAIM PAULISTA", "GUAIANASES", "GUAIANASES", "GRAJAÚ", "GRAJAÚ", 
"PIRAPORINHA", "PIRAPORINHA", "CAPÃO REDONDO", "CAPÃO REDONDO", 
"RIO PEQUENO", "RIO PEQUENO", "SÃO MATEUS", "SÃO MATEUS", "BRASILÂNDIA", 
"BRASILÂNDIA", "PARELHEIROS", "PARELHEIROS", "PERUS", "PERUS", 
"CANGAÍBA", "CANGAÍBA", "PONTE RASA", "PONTE RASA", "JARDIM HELENA", 
"JARDIM HELENA", "VILA JACUÍ", "VILA JACUÍ", "JARAGUÁ", "JARAGUÁ", 
"CIDADE TIRADENTES", "CIDADE TIRADENTES", "CONJUNTO JOSÉ BONIFÁCIO", 
"CONJUNTO JOSÉ BONIFÁCIO", "JARDIM SÃO LUÍS", "JARDIM SÃO LUÍS", 
"CURSINO", "CURSINO", "PARQUE DO CARMO", "PARQUE DO CARMO", "PEDREIRA", 
"PEDREIRA", "VILA SABRINA", "VILA SABRINA", "TEOTÔNIO VILELA", 
"TEOTÔNIO VILELA", "LAUZANE PAULISTA", "LAUZANE PAULISTA"), 
    values = c(2319L, 10746L, 12740L, 3064L, 1526L, 4472L, 4217L, 
    8152L, 5152L, 1769L, 2975L, 9747L, 2394L, 11559L, 2397L, 
    5630L, 2132L, 5452L, 3135L, 8980L, 2717L, 5490L, 3515L, 9074L, 
    1358L, 6739L, 6329L, 2576L, 10638L, 5886L, 6613L, 3820L, 
    9205L, 4131L, 7914L, 3951L, 4448L, 8885L, 7117L, 3723L, 8398L, 
    2842L, 8667L, 4003L, 9427L, 3077L, 9481L, 3361L, 5642L, 2256L, 
    5224L, 2155L, 6901L, 2513L, 14567L, 2998L, 2560L, 8058L, 
    8832L, 4382L, 4540L, 2681L, 3030L, 5928L, 6599L, 3219L, 6101L, 
    2405L, 8453L, 3771L, 8698L, 3097L, 2633L, 11795L, 18298L, 
    3803L, 2148L, 8862L, 14603L, 4730L, 4658L, 12708L, 4260L, 
    12008L, 3042L, 11926L, 11475L, 3324L, 2803L, 6373L, 6822L, 
    2695L, 3431L, 6628L, 1833L, 4455L, 5805L, 2061L, 2850L, 10948L, 
    6657L, 1943L, 11526L, 2762L, 3287L, 7874L, 2712L, 7242L, 
    3084L, 8638L, 2860L, 5376L, 6664L, 2870L, 3097L, 7166L), 
    text = c("E", "G", "G", "E", "E", "G", "E", "G", "G", "E", 
    "E", "G", "E", "G", "E", "G", "E", "G", "E", "G", "E", "G", 
    "E", "G", "E", "G", "G", "E", "G", "E", "G", "E", "G", "E", 
    "G", "E", "E", "G", "G", "E", "G", "E", "G", "E", "G", "E", 
    "G", "E", "G", "E", "G", "E", "G", "E", "G", "E", "E", "G", 
    "G", "E", "G", "E", "E", "G", "G", "E", "G", "E", "G", "E", 
    "G", "E", "E", "G", "G", "E", "E", "G", "G", "E", "E", "G", 
    "E", "G", "E", "G", "G", "E", "E", "G", "G", "E", "E", "G", 
    "E", "G", "G", "E", "E", "G", "G", "E", "G", "E", "E", "G", 
    "E", "G", "E", "G", "E", "G", "G", "E", "E", "G")), row.names = c(NA, 
116L), class = "data.frame")

This works:

ggplot() +
  geom_col(data = d,
           aes(x = text, 
               y = values, 
               fill = text)) +
  facet_wrap(~name) 

enter image description here

But, this does not:

ggplot() +
  geom_col(data = d,
           aes(x = text, 
               y = values, 
               fill = text)) +
  geofacet::facet_geo(~name, grid = "mygrid") 

The error message is: Error in get_grid(grid) : grid 'mygrid' not recognized...

What am I missing here?

2

There are 2 answers

0
Arthur Welle On BEST ANSWER

I will answer my own question with the help of Hannah.

There are two problems in the question code.

1. Quotes

First problem is the one spoted by Hannah. Following the package examples one would do:

ggplot(state_ranks, aes(variable, rank, fill = variable)) +
  geom_col() +
  coord_flip() +
  facet_geo(~ state, grid = "us_state_grid2") +
  theme(panel.spacing = unit(0.1, "lines"))

but when dealing with a custom grid we have to take the quotes out:

mygrid <- geofacet::us_state_grid2

ggplot(state_ranks, aes(variable, rank, fill = variable)) +
  geom_col() +
  coord_flip() +
  facet_geo(~ state, grid = mygrid) +
  theme(panel.spacing = unit(0.1, "lines"))

I don´t know why it has to be this way, a note in the package documentationn would be helpful.

2. Passing the dataset

In this github issue we learn that the geom facet_geo has to receave the data as well, so it´s necessary to pass the data in the first ggplot call not only in the geom.

Note that using a more commom facet_wrap the following is a perfect valid ggplot:

ggplot() +
  geom_col(data = state_ranks, 
           aes(x = variable, y = rank, fill = variable)) +
  coord_flip() +
  facet_wrap(~state) +
  theme(panel.spacing = unit(0.1, "lines"))

But the following fails:

ggplot() +
  geom_col(data = state_ranks, 
           aes(x = variable, y = rank, fill = variable)) +
  coord_flip() +
  facet_geo(~ state, grid = "us_state_grid2") +
  theme(panel.spacing = unit(0.1, "lines"))

One have to write instead:

ggplot(data = state_ranks) +
  geom_col(aes(x = variable, y = rank, fill = variable)) +
  coord_flip() +
  facet_geo(~ state, grid = "us_state_grid2") +
  theme(panel.spacing = unit(0.1, "lines"))
1
Hannah Koenker On

I think you need:

geofacet::facet_geo(~name, grid = mygrid) 

i.e. mygrid without the quotation marks.