-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
276 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.