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

add basic print.xmap_df() Method #60

Closed cynthiahqy closed 1 year ago

cynthiahqy commented 1 year ago

print.xmap()

Existing methods:

The properties of a crossmap that might be useful to display:

Originally posted by @cynthiahqy in https://github.com/cynthiahqy/conformr-project/issues/43#issuecomment-1396445724

cynthiahqy commented 1 year ago
> igraph::print_all(b_igraph)
IGRAPH c778a63 DNWB 19 13 -- 
+ attr: type (v/l), name (v/c), weight (e/n)
+ edges from c778a63 (vertex names):
 [1] a->AA b->AA c->AA d->AA e->BB f->BB g->CC h->DD i->EE i->FF j->GG j->HH j->II

> print_all(b_igraph, vertex.attributes = TRUE)
IGRAPH c778a63 DNWB 19 13 -- 
+ attr: type (v/l), name (v/c), weight (e/n)
+ vertex attributes:
|       type name
| [1]  FALSE    a
| [2]  FALSE    b
| [3]  FALSE    c
| [4]  FALSE    d
| [5]  FALSE    e
| [6]  FALSE    f
| [7]  FALSE    g
| [8]  FALSE    h
| [9]  FALSE    i
| [10] FALSE    j
| [11]  TRUE   AA
| [12]  TRUE   BB
| [13]  TRUE   CC
| [14]  TRUE   DD
| [15]  TRUE   EE
| [16]  TRUE   FF
| [17]  TRUE   GG
| [18]  TRUE   HH
| [19]  TRUE   II
+ edges from c778a63 (vertex names):
 [1] a->AA b->AA c->AA d->AA e->BB f->BB g->CC h->DD i->EE i->FF j->GG j->HH j->II

From ?igraph::print.igraph() and snooping code https://github.com/igraph/rigraph/blob/dd36324d2338ef5edc80061c2dae73dec4161841/R/print.R#L63-L75

Aside on igraph library:

cynthiahqy commented 1 year ago

More print designs from igraph print.R:

## This is good if vertices are named

IGRAPH UNW- 10 18 -- Krackhardt kite
+ attributes: name (g/c), name (v/c), weight (e/n)
+ edges:
Andre    -- [1] Beverly, Carol, Diane, Fernando
Beverly  -- [1] Andre, Diane, Ed, Garth
Carol    -- [1] Andre, Diane, Fernando
Diane    -- [1] Andre, Beverly, Carol, Diane, Ed
         -- [6] Garth
Ed       -- [1] Beverly, Diane, Garth
Fernando -- [1] Andre, Carol, Diane, Garth
Garth    -- [1] Beverly, Diane, Ed, Fernando
Heather  -- [1] Fernando, Garth
Ike      -- [1] Heather, Jane
Jane     -- [1] Ike
IGRAPH-UNW--V5-E5----------------------------------------- A ring -
+ attributes: name (g), name (v), weight (e).
+ edges:
     edge  weight
[1]' a--b       1
[2]' b--c       2
[3]' c--d      -1
[4]' d--e     0.5
[5]' a--e       1
cynthiahqy commented 1 year ago

tidygraph tbl_graph print:

library(tidygraph)

play_erdos_renyi(10, 0.5) %>% 
  activate(nodes) %>% 
  mutate(degree = centrality_degree()) %>% 
  activate(edges) %>% 
  mutate(centrality = centrality_edge_betweenness()) %>% 
  arrange(centrality)
#> # A tbl_graph: 10 nodes and 39 edges
#> #
#> # A directed simple graph with 1 component
#> #
#> # Edge Data: 39 × 3 (active)
#>    from    to centrality
#>   <int> <int>      <dbl>
#> 1     4    10       1.25
#> 2     2    10       1.92
#> 3     6    10       2.08
#> 4     5     2       2.17
#> 5     5    10       2.25
#> 6     4     6       2.25
#> # … with 33 more rows
#> #
#> # Node Data: 10 × 1
#>   degree
#>    <dbl>
#> 1      4
#> 2      4
#> 3      2
#> # … with 7 more rows
cynthiahqy commented 1 year ago

xmap_df are essentially edge lists, with weight & type attributes in igraph:

links <- tibble::tribble(~from, ~to, ~weights,
                "A1", "B01", 0.5,
                "A1", "B02", 0.5,
                "A2", "B03", 1,
                "A3", "B04", 1,
                "A4", "B04", 1)

g_xmap_weight <-
  links |>
  dplyr::rename(weight = weights) |> ## rename to make weighted graph
  igraph::graph_from_data_frame()

igraph::print_all(g_xmap_weight, edge = TRUE)
#> IGRAPH 82e84dd DNW- 8 5 -- 
#> + attr: name (v/c), weight (e/n)
#> + edges (vertex names) and their attributes:
#>         edge weight
#> [1] A1 ->B01    0.5
#> [2] A1 ->B02    0.5
#> [3] A2 ->B03    1.0
#> [4] A3 ->B04    1.0
#> [5] A4 ->B04    1.0

tidygraph is a wrapper around igraph

tidygraph::as_tbl_graph(g_xmap_DNWB)
#> # A tbl_graph: 8 nodes and 5 edges
#> #
#> # A rooted forest with 3 trees
#> #
#> # Node Data: 8 × 2 (active)
#>   name  type 
#>   <chr> <lgl>
#> 1 A1    TRUE 
#> 2 A2    TRUE 
#> 3 A3    TRUE 
#> 4 A4    TRUE 
#> 5 B01   FALSE
#> 6 B02   FALSE
#> # … with 2 more rows
#> #
#> # Edge Data: 5 × 3
#>    from    to weight
#>   <int> <int>  <dbl>
#> 1     1     5    0.5
#> 2     1     6    0.5
#> 3     2     7    1  
#> # … with 2 more rows
tidygraph::as_tbl_graph(g_xmap_DNWB) |> class()
#> [1] "tbl_graph" "igraph"  

Created on 2023-01-23 with reprex v2.0.2

cynthiahqy commented 1 year ago

xmap_df print out should show:

#> # A xmap_df: 
#> # from <col_from> to <col_to> with
#> # <one-to-one/one-to-many/many-from-one> assignments
#> # 
#> # "ISIC2" -> "ISIC3" | "shares"
#>         link | weight
#> [1] A1 ->B01  |  0.5
#> [2] A1 ->B02  |  0.5
#> [3] A2 ->B03  |  1.0
#> [4] A3 ->B04  |  1.0
#> [5] A4 ->B04  |  1.0
#> ... with <0> more links

Alternative header & footer:

#> # A xmap_df: 
#> # <5> links from <4> <col_from> to <4> <col_to> categories
#> # with <recode {{,/and} split} {and aggregate}> mappings

#> ...with <0> more links between <4> source and <4> target nodes
cynthiahqy commented 1 year ago

Useful resource for methods/generics implementation (with examples):

cynthiahqy commented 1 year ago

Check the arguments of the generic function:

args(print)

Make a valid xmap:

df <- tibble::tribble(
      ~from, ~to, ~weights,
      "A1", "B01", 1,
      "A2", "B02", 1,
      "A3", "B02", 1,
      "A4", "B03", 0.67,
      "A4", "B04", 0.33
    )

Default Printing Methods:

## tibble
pillar:::print.tbl(df)

## data.frame
base::print.data.frame(df)
print.xmap_df <- function(x, ...) {
  cat("Using the print.xmap_df method")
}
cynthiahqy commented 1 year ago

basic method prints some info, possible enhancements have been logged as issues #89