Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow-up to #353, create a geofacet with Guerry data #403

Open
friendly opened this issue May 16, 2023 · 0 comments
Open

Follow-up to #353, create a geofacet with Guerry data #403

friendly opened this issue May 16, 2023 · 0 comments

Comments

@friendly
Copy link

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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant