I am having difficulties using a custom grid in the
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)
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?
CodePudding user response:
I think you need:
geofacet::facet_geo(~name, grid = mygrid)
i.e. mygrid without the quotation marks.
CodePudding user response:
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"))