hafen / geofacet

R package for geographical faceting with ggplot2
https://hafen.github.io/geofacet/
Other
333 stars 43 forks source link

help using geofacet with Guerry data, map of France #353

Open friendly opened 2 years ago

friendly commented 2 years ago

Can someone help me get started using the geofacet package, fr_departements_grid1 to plot one or more of the variables in the Guerry package.

`> data(Guerry)

str(Guerry) 'data.frame': 86 obs. of 23 variables: $ dept : int 1 2 3 4 5 7 8 9 10 11 ... $ Region : Factor w/ 5 levels "C","E","N","S",..: 2 3 1 2 2 4 3 4 2 4 ... $ Department : Factor w/ 86 levels "Ain","Aisne",..: 1 2 3 11 40 4 5 6 7 8 ... $ Crime_pers : int 28870 26226 26747 12935 17488 9474 35203 6173 19602 15647 ... $ Crime_prop : int 15890 5521 7925 7289 8174 10263 8847 9597 4086 10431 ... $ Literacy : int 37 51 13 46 69 27 67 18 59 34 ... $ Donations : int 5098 8901 10973 2733 6962 3188 6400 3542 3608 2582 ... $ Infants : int 33120 14572 17044 23018 23076 42117 16106 22916 18642 20225 ... $ Suicides `

stedy commented 2 years ago

What have you tried so far? You might have better luck on Stack Overflow

friendly commented 2 years ago

Yes, I should have tried something first and then commented on what didn't work. You can close this issue.

friendly commented 2 years ago

OK, so I've tried using the two French grids, fr_departements_grid1 (101 depts) and fr_departements_grid2 (69 depts). I can post to SO, but trying again here for some direct pointers.

My Guerry data is based on SpatialPolygonsDataFrame maps of France in 1830, Guerry::gfrance with 86 depts and Guerry::gfrance85 (w/o Corsica)

The key variable in my data frames is dept, an integer, I guess corresponding to code in your grids. But neither of your grids match my maps. I tried:

ggplot(df_ranks, aes(Crime_pers)) +
    geom_point() +
    facet_geo(~dept, grid = "fr_departements_grid2")

but this gives errors

# Some values in the specified facet_geo column 'dept' do not match the 'code' column of the specified grid and will be removed: 1,
# 2, 3, 4, 5, 7, 8, 9, 17, 22, 23, 29, 50, 55, 59, 61, 62, 69, 70, 78, 87, 89, 200
# Error in `levels<-`(`*tmp*`, value = as.character(levels)) : 
#   factor level [33] is duplicated

I guess I need to create a new grid for my maps from 1830, but have no idea how to do this.

stedy commented 2 years ago

yes, I think you are correct. There is a tool to create and export a grid here: https://hafen.github.io/grid-designer/