cynthiahqy / conformr-xmap-project

R Package for harmonising data of different classifications or aggregations into a single dataset
MIT License
7 stars 1 forks source link

write helper for matrix plots of xmap #128

Open cynthiahqy opened 1 year ago

cynthiahqy commented 1 year ago

Autoplot and plot return bigraphs (in #56), but matrix visualisation is useful for explaining why crossmaps are useful; but do I want to offer this functionality in the package??

Matrix plot

library(tidyverse)
library(ggplot)
library(ggbump)

edges <- tibble::tribble(~key1, ~key2, ~share,
                 "BLX", "BEL", 0.5,
                 "BLX", "LUX", 0.5,
                 "E.GER", "DEU", 1,
                 "W.GER", "DEU", 1,
                 "AUS", "AUS", 1)

Make missing links explicit:

inc_tbl <- expand_grid(key1 = edges$key1, key2 = edges$key2) |> 
    left_join(edges, by = c("key1", "key2"))

Add cases to facilitate colour of matrix cells, and plot matrix.

plt_inc_long_mtx <- function(inc_long, from, to, weights) {
  gg <- inc_long |>
    dplyr::mutate(src_case = dplyr::case_when(
      {{weights}}==1 ~ "one-to-one",
      is.na({{weights}}) ~ "none",
      {{weights}} < 1 ~ "one-to-many")) |>
  ggplot(aes(x={{to}}, y={{from}})) +
    geom_tile(aes(fill=src_case), col="grey") +
    scale_y_discrete(limits=rev) +
    scale_x_discrete(position='top') +
    scale_fill_brewer() +
    coord_fixed()  +
    labs(x = element_blank(), y = element_blank(), fill="source-to-target") +
    theme_minimal()
  return(gg)
}

plt_inc_long_mtx(inc_tbl, key1, key2, share)

image

Originally posted by @cynthiahqy in https://github.com/cynthiahqy/conformr-xmap-project/issues/56#issuecomment-1386905773

cynthiahqy commented 1 year ago