Data.frame to connection graph (reprex enclosed)

HI,

I am facing with the follwoing issue: I have a data.frame where I know that which topic is originated from which one, but I would like to create a figure about the evolution.

At the enclosed example k is the step (I would prefer it on the x-axis), and prev is the parent topic.

Thanks in advance,
M

tibble::tribble(
  ~current_topic, ~prev_topic, ~k,
              3L,          1L, 10,
              9L,          3L, 10,
              7L,          4L, 10,
              5L,          5L, 10,
              2L,          2L, 10,
              6L,          6L, 10,
              8L,          8L, 10,
             10L,          7L, 10,
              4L,          7L, 10,
              1L,          8L, 10,
              3L,          3L,  8,
              8L,          5L,  8,
              1L,          1L,  8,
              5L,          2L,  8,
              6L,          4L,  8,
              2L,          3L,  8,
              7L,          6L,  8,
              4L,          6L,  8,
              4L,          3L,  6,
              1L,          1L,  6,
              6L,          4L,  6,
              5L,          2L,  6,
              2L,          2L,  6,
              3L,          1L,  6,
              2L,          2L,  4,
              1L,          2L,  4,
              3L,          1L,  4,
              4L,          1L,  4
  )
#> # A tibble: 28 × 3
#>    current_topic prev_topic     k
#>            <int>      <int> <dbl>
#>  1             3          1    10
#>  2             9          3    10
#>  3             7          4    10
#>  4             5          5    10
#>  5             2          2    10
#>  6             6          6    10
#>  7             8          8    10
#>  8            10          7    10
#>  9             4          7    10
#> 10             1          8    10
#> # … with 18 more rows

Created on 2022-04-12 by the reprex package (v2.0.1)

if your data is df0


df1 <-  df0 %>% mutate(k=as.integer(factor(k)),
             name = paste0(current_topic,"_",k),
             parent=paste0(prev_topic,"_",k-1),
             id=row_number()
             ) %>% select(from=name,to=parent)


library(visNetwork)
(node_df <- pivot_longer(df1, cols = everything()) %>%
  select(id = value,label=value) %>%
  distinct() %>%
  separate(col = label, sep = "_", into = c("discard", "level")) %>%
    mutate(label=id) %>% select(-discard)) 

visNetwork(nodes = node_df,
           edges = df1)%>% 
  visEdges(arrows = "from") %>% 
  visHierarchicalLayout(direction="LR")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.