Last updated: 2022-11-22

Checks: 6 1

Knit directory: viz-panel-maps/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20221110) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 92bf67a. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    README_files/

Untracked files:
    Untracked:  analysis/sketch-example.Rmd
    Untracked:  code/sketch-example.R
    Untracked:  renv/

Unstaged changes:
    Modified:   .Rprofile
    Modified:   analysis/index.Rmd
    Modified:   analysis/stylised-example.Rmd
    Modified:   renv.lock

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


For SoDa presentation

Setup Sketch Example

# sample data
df_data_in <- tibble::tribble(~source_code, ~value_in,
                                "x1111", 100,
                                "x2222", 30,
                                "x3333", 20,
                                "x4444", 80,
                                "x5555", 30,
                                "x6666", 40,
                                "x7777", 15
                                )
df_concordance <- tibble::tribble(~source_code, ~target_code,
                                "x1111", "A1",
                                "x2222", "B2",
                                "x2222", "B3",
                                "x3333", "C5",
                                "x4444", "C5",
                                "x5555", "D6",
                                "x5555", "D7",
                                "x6666", "D6",
                                "x6666", "D7",
                                "x7777", "D6"
                                )

(df_pm <- df_concordance |> 
  conformr::make_panel_map_equal(code_in = source_code, code_out = target_code, "weights"))
# A tibble: 10 × 3
   source_code target_code weights
   <chr>       <chr>         <dbl>
 1 x1111       A1              1  
 2 x2222       B2              0.5
 3 x2222       B3              0.5
 4 x3333       C5              1  
 5 x4444       C5              1  
 6 x5555       D6              0.5
 7 x5555       D7              0.5
 8 x6666       D6              0.5
 9 x6666       D7              0.5
10 x7777       D6              1  
(data_out <-
  conformr::use_panel_map(map = df_pm,
                data = df_data_in, values_from = value_in,
                from_code = source_code, to_code = target_code,
                weights = weights, .suffix = "_out"))
# A tibble: 6 × 2
  target_code value_in_out
  <chr>              <dbl>
1 A1                   100
2 B2                    15
3 B3                    15
4 C5                   100
5 D6                    50
6 D7                    35

Visualisations

# viz panel map
plt_panel_map <- function(pm, from, to, weighted){
  require(dplyr)
  require(ggplot2)
  require(ggbump)
  edges <- pm |>
    transmute(from = {{from}}, to = {{to}}, weighted = {{weighted}})  
  
  ## calculate positions for nodes
  from_nodes <- distinct(edges, from) |> mutate(from_y = row_number())
  to_nodes <- distinct(edges, to) |> mutate(to_y = row_number() - 1 + 0.5)
  
  ## generate df for ggplot
  df <- edges |>
    ## generate mapping type/case variables
    group_by(from) |> 
    mutate(n_dest = n()) |>
    ungroup() |>
    group_by(to) |> 
    mutate(n_origin = n(),
           min_weight = min(weighted)) |>
    ungroup() |>
    mutate(value_case = case_when(n_dest == 1 ~ "one-to-one",
                                  n_dest > 1 ~ "one-to-many")) |>
    left_join(tribble(~value_case, ~line_type, ~font_type,
                      "one-to-one", "solid", "bold",
                      "one-to-many", "dashed", "italic"),
              by = "value_case") |>
    mutate(from_case = case_when(n_origin == 1 ~ "one-from-one",
                                 n_origin > 1 ~ "one-from-many",
                                 n_origin < 1 ~ "ERROR! origin codes < 1"),
           dest_case = case_when(min_weight < 1 ~ "contains split",
                                 min_weight == 1 ~ "aggregation only",
                                 min_weight > 1 ~ "ERROR! weight > 1")
    ) |> 
    ## add y-coordinates
    left_join(from_nodes, by = "from") |>
    left_join(to_nodes, by = "to") |>
    ## add x-coordinates
    mutate(from_x = 0,
           to_x = 5) |>
    ## give each from-out instruction a unique id
    mutate(idx = row_number())
  
plt_uw <- df |>
  ggplot(aes(x = from_x, xend = to_x, y = from_y, yend = to_y, group = idx)) +
  ## edges as sigmoid curves with line type
  geom_sigmoid(aes(linetype = I(line_type))) +
  # to/from nodes
  scale_y_reverse() +
  geom_text(aes(x = from_x - 0.5, label=from, fontface=I(font_type))) +
  geom_label(aes(x = to_x + 0.5, y = to_y, label=to, fill = dest_case)) +
  # edge labels
  geom_label(data = filter(df, value_case == "one-to-many"),
             aes(x = (((from_x + to_x) / 2) + to_x) / 2,
                 y = to_y,
                 label = weighted)) +
  geom_label(data = filter(df, value_case == "one-to-one"),
             aes(x = (from_x + to_x) / 4,
                 y = from_y,
                 label = weighted)) +
  # theme
  scale_fill_manual(values = wesanderson::wes_palette(n = 4, name = "GrandBudapest2")) +
  scale_color_manual(values = wesanderson::wes_palette(n = 4, name = "GrandBudapest2")) +
  cowplot::theme_minimal_grid(font_size = 14, line_size = 0) +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        plot.background = element_rect(fill = "white")) +
  labs(x = NULL, y = NULL, fill = "Output Relation")

return(plt_uw)
}

df_pm |>
  plt_panel_map(from = source_code, to = target_code, weighted = weights)
Loading required package: dplyr

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
Loading required package: ggplot2
Loading required package: ggbump

without code…

Pretty Tables

Sorted by target_code

df_pm |>
  dplyr::arrange(target_code)
# A tibble: 10 × 3
   source_code target_code weights
   <chr>       <chr>         <dbl>
 1 x1111       A1              1  
 2 x2222       B2              0.5
 3 x2222       B3              0.5
 4 x3333       C5              1  
 5 x4444       C5              1  
 6 x5555       D6              0.5
 7 x6666       D6              0.5
 8 x7777       D6              1  
 9 x5555       D7              0.5
10 x6666       D7              0.5

sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggbump_0.1.0  ggplot2_3.3.6 dplyr_1.0.10 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9          highr_0.9           pillar_1.8.1       
 [4] compiler_4.2.1      bslib_0.3.1         later_1.3.0        
 [7] jquerylib_0.1.4     git2r_0.30.1        workflowr_1.7.0    
[10] tools_4.2.1         digest_0.6.30       gtable_0.3.1       
[13] jsonlite_1.8.2      evaluate_0.16       lifecycle_1.0.3    
[16] tibble_3.1.8        pkgconfig_2.0.3     rlang_1.0.6        
[19] DBI_1.1.3           cli_3.4.1           rstudioapi_0.13    
[22] yaml_2.3.5          xfun_0.31           fastmap_1.1.0      
[25] withr_2.5.0         stringr_1.4.1       conformr_0.0.0.9001
[28] knitr_1.39          generics_0.1.3      fs_1.5.2           
[31] vctrs_0.4.2         sass_0.4.2.9000     cowplot_1.1.1      
[34] grid_4.2.1          rprojroot_2.0.3     tidyselect_1.2.0   
[37] glue_1.6.2          R6_2.5.1            fansi_1.0.3        
[40] wesanderson_0.3.6   rmarkdown_2.14      farver_2.1.1       
[43] magrittr_2.0.3      scales_1.2.1        promises_1.2.0.1   
[46] htmltools_0.5.2     assertthat_0.2.1    colorspace_2.0-3   
[49] httpuv_1.6.5        labeling_0.4.2      utf8_1.2.2         
[52] stringi_1.7.8       munsell_0.5.0