Follow-up to #353, create a geofacet with Guerry data
friendly opened this issue · 4 comments
I'm re-opening this query #353 after I have what I think is a solution, but I get an error when I try to use it.
The code below creates gfrance85_grid
from the map data Guerry::gfrance85
.
#' ---
#' title: Create gfrance grid for geofacet package
#' ---
#'
library(geofacet)
library(ggplot2)
library(dplyr)
library(tidyr)
library(Guerry)
# scale a variable into n integer bins
bin <- function(x, n=10){
1 + floor(n * (x - min(x)) / (max(x) - min(x)))
}
data(gfrance85, package = "Guerry")
dept <- data.frame(gfrance85)[,"dept"]
dep.names <- data.frame(gfrance85)[,"Department"]
region.names <- data.frame(gfrance85)[,"Region"]
# extract department centroids
xy <- coordinates(gfrance85) |>
as.data.frame() |>
setNames(c("x", "y"))
gfrance85_grid <- data.frame(code = dept,
name = as.character(dep.names),
row = bin(xy$x),
col = bin(xy$y))
head(gfrance85_grid)
grid_preview("gfrance85_grid") +
geom_text(aes(label=name), size=2, nudge_y=0.35)
This looks OK to me:
> head(gfrance85_grid)
code name row col
1 1 Ain 8 4
2 2 Aisne 6 8
3 3 Allier 6 4
4 4 Basses-Alpes 9 1
5 5 Hautes-Alpes 9 2
6 7 Ardeche 7 2
grid_preview
gives me this error, suggesting it does not accept an appropriate grid data frame as input, but instead looks inside the package:
> grid_preview("gfrance85_grid") +
+ geom_text(aes(label=name), size=2, nudge_y=0.35)
grid 'gfrance85_grid' not found in package, checking online...
Error in get_grid(x) : grid 'gfrance85_grid' not recognized...
I also tried the grid designer, uploading my grid as a .csv file. It gave me this error:
There are duplicates of the following (row,col) indices: (9,1), (7,8), (4,0), (5,0), (8,1), (5,3), (4,8), (2,7), (4,5), (6,3), (1,6), (8,7), (9,7), (5,7), (3,5). Please correct this by editing the csv above and repopulate.
I guess I have to try more bins, but I'm still stumped on how to use my own grid.
I posted a revision of the code above, but using a 17 x 17 grid on SO, https://stackoverflow.com/questions/76268255/how-to-create-a-geofacet-grid-from-the-guerry-map-of-france-in-1830
Sorry to persist, but I'd really like to solve this. I'm attaching the .csv file for my 17 x 17 grid. There are no duplicates.
gfrance85_grid.csv
grid_preview()
refuses to accept this:
> grid_preview("gfrance85_grid") +
+ geom_text(aes(label=name), size=2, nudge_y=0.35)
grid 'gfrance85_grid' not found in package, checking online...
Error in get_grid(x) : grid 'gfrance85_grid' not recognized...
I also tried to use your GeoGrid Designer with this csv file. But it gives me:
All row and column indices must be positive. Please correct this by editing the csv above and repopulate.
How can I make this work?
You need to send the data frame object gfrance85_grid
to grid_preview()
, not the string "gfrance85_grid"
. If you send a string it searches the collection of grids that have already been contributed to the package.
However, if you send the data frame, you will get the following error:
grid_preview(gfrance85_grid) +
geom_text(aes(label = name), size = 2, nudge_y = 0.35)
# Error: A custom grid must have unique row/column locations for each entry
There are several entries that share the same grid cell:
count(gfrance85_grid, row, col) %>% arrange(desc(n))
# row col n
# 1 2 7 2
# 2 3 8 2
# 3 4 6 2
# 4 5 1 2
# 5 5 6 2
# 6 5 9 2
# 7 6 1 2
# 8 6 4 2
# 9 6 8 2
# 10 7 4 2
# 11 8 9 2
# 12 9 2 2
# 13 9 8 2
# 14 10 2 2
# 15 10 8 2
# 16 1 8 1
# 17 2 8 1
# ...
Have you tried using grid_auto()
?