Skip to content

Commit

Permalink
add Guerry_ranks; test geofacet
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Dec 13, 2024
1 parent 0b1000e commit 5414b65
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 113 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: Guerry
Type: Package
Title: Maps, Data and Methods Related to Guerry (1833) "Moral Statistics of France"
Version: 1.8.3
Date: 2023-10-24
Version: 1.8.4
Date: 2024-12-13
Authors@R: c(
person(given = "Michael",
family = "Friendly",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 1.8.4 (2024-12-13)

* Added Guerry_ranks, with all numeric variables ranked

## Version 1.8.3 (2023-10-13)

A problem with the adegraphics package becoming archived triggered a warning on Guerry
Expand Down
29 changes: 29 additions & 0 deletions data-raw/Guerry_ranks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

data(Guerry, package="Guerry")

Guerry_ranks <- Guerry |>
mutate(
Crime_pers = dense_rank(Crime_pers),
Crime_prop = dense_rank(Crime_prop),
Literacy = dense_rank(Literacy),
Donations = dense_rank(Donations),
Infants = dense_rank(Infants),
Suicides = dense_rank(Suicides)
)

# do it for all numeric
Guerry_ranks<- Guerry |>
mutate(across(!dept & where(is.numeric), dense_rank))

save(Guerry_ranks, file=here::here("data", "Guerry_ranks.RData"))


# transform main variables to long, fur some uses

library(tidyr)

Guerry_ranks_long <- Guerry_ranks |>
select(dept:Suicides) |>
pivot_longer(cols=Crime_pers:Suicides,
names_to = "variable",
values_to = "rank")
Binary file added data/Guerry_ranks.RData
Binary file not shown.
File renamed without changes.
68 changes: 43 additions & 25 deletions examples/geofacet-ex1.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,84 @@ bin <- function(x, n=10){

data(gfrance85, package = "Guerry")

# extract department info
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"))

# make the grid data.frame

nbin <- 17
gfrance85_grid <- data.frame(code = dept,
name = as.character(dep.names),
col = bin(xy$x, nbin),
row = bin(xy$y, nbin)
) +
mutate(row = nbin - row)
# region = region.names)
# mutate(region = factor(region,
# labels =c("Central", "East", "North", "South", "West")))
#head(gfrance85_grid)
gfrance85_grid <- data.frame(
# row = bin(xy$y, nbin),
row = nbin - bin(xy$y, nbin) + 2 ,
col = bin(xy$x, nbin),
code = dept,
name = as.character(dep.names),
name_region = factor(region.names, labels =c("Central", "East", "North", "South", "West"))
)


names(gfrance85_grid)

# check for duplicates
which(duplicated(gfrance85_grid[, c("row", "col")]))

#count(gfrance85_grid, row, col) %>% arrange(desc(n))


range(gfrance85_grid$row)
range(gfrance85_grid$col)

write.csv(gfrance85_grid, file = "examples/gfrance85_grid.csv", row.names = FALSE)


# gfrance85_grid |>
# mutate(label = paste(code, "\n", name)) |>
# ggplot(aes(x = row, y = col)) +
# geom_tile(color = "black",
# fill = scales::alpha("blue", .3)) +
# geom_text(aes(label = label), size = 2) +
# coord_equal()

col.region <- colors()[c(149,254,468,552,26)] |>
scales::alpha(alpha = 0.2)

gfrance85_grid |>
ggplot(aes(x = col, y = row, fill = region.names)) +
ggplot(aes(x = col, y = row, fill = name_region)) +
geom_tile(color = "black") +
geom_text(aes(label = code), size = 3, nudge_y = .2) +
geom_text(aes(label = code), size = 4, nudge_y = .2) +
geom_text(aes(label = name), size = 2, nudge_y = -.2) +
coord_equal()

scale_y_reverse() +
coord_equal() +
theme_minimal(base_size = 14) +
theme(legend.position = "none")


write.csv(gfrance85_grid, file = "examples/gfrance85_grid.csv", row.names = FALSE)

grid_preview(gfrance85_grid, label="name")

grid_preview(gfrance85_grid) +
geom_text(aes(label=name), size=2, nudge_y=0.35) +

#Error: Other than 'row' and 'col', variable names of a custom grid must begin with 'code' or 'name'

# works, if I remove 'region'
grid_preview(gfrance85_grid[, -5]) +
geom_text(aes(label=name), size=2, nudge_y=0.35)


# try grid_auto

grd <- grid_auto(gfrance85, names = "Department", codes = "dept", seed = 47)
range(grd$row)
range(grd$col)

grid_preview(grd, label = "name_Department") +
geom_text(aes(label=name), size=2, nudge_y=0.35)


# other faceted maps of france
# 7 x 7, including overseas
str(fr_regions_grid1)
grid_preview(fr_regions_grid1)

str(fr_departements_grid1)
grid_preview(fr_departements_grid1)
3 changes: 3 additions & 0 deletions examples/geofacet-ex2.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ library(Guerry)

gfrance85_grid <- read.csv(file = "examples/gfrance85_grid.csv")

gfrance85_grid <- gfrance85_grid |>
rename(name.region = region)

range(gfrance85_grid$row)
range(gfrance85_grid$col)

grid_preview(gfrance85_grid) +
aes(color = name.region) +
geom_text(aes(label=name), size=2, nudge_y=0.35)

# need to reverse the rows to table order
Expand Down
29 changes: 29 additions & 0 deletions examples/geofacet-test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

gfrance85_grid <- read.csv(file = "examples/gfrance85_grid.csv")

data(Guerry_ranks)

library(dplyr)
library(tidyr)
library(geofacet)
library(ggplot2)

# documentation example
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"))


Guerry_ranks_long <- Guerry_ranks |>
select(dept:Suicides) |>
pivot_longer(cols=Crime_pers:Suicides,
names_to = "variable",
values_to = "rank")

ggplot(Guerry_ranks_long, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ dept, grid = gfrance85_grid) +
theme(panel.spacing = unit(0.1, "lines"))
64 changes: 64 additions & 0 deletions examples/geogacet-make_grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' ---
#' title: Try making grids
#' ---

library(sp)
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(gfrance, package = "Guerry")
data(gfrance85, package = "Guerry")

# extract department info
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"))

make_grid <- function(nbin) {
grid <- data.frame(
# row = bin(xy$y, nbin),
row = nbin - bin(xy$y, nbin) + 2 ,
col = bin(xy$x, nbin),
code = dept,
name = as.character(dep.names),
name_region = factor(region.names, labels =c("Central", "East", "North", "South", "West"))
)
dups <- which(duplicated(grid[, c("row", "col")]))
cat("There are", sum(dups), "duplicates\n")
if(sum(dup) > 0) {
count(grid, row, col) |>
arrange(desc(n)) |>
filter(n > 1) |> print()
}
cat("Ranges:\n")
cat("\trow:", range(grid$row))
cat("\tcol:", range(grid$col), "\n")

return(grid)
}

gfrance85_grid17 <- make_grid(17)
gfrance85_grid16 <- make_grid(16)
gfrance85_grid15 <- make_grid(15)

grid_preview(gfrance85_grid15) +
geom_text(aes(label=name), size=2, nudge_y=0.35)

#try the designer
grid_design(gfrance85_grid17,
img = "/~https://github.com/friendly/Guerry/blob/master/man/figures/README-gfrance85-labels-1.png?raw=true",
label = "code")
Loading

0 comments on commit 5414b65

Please sign in to comment.