vignettes/economic-activity-hierarchy.R

## ----setup, include = FALSE----------------------------------------------
library(magrittr)
library(ggplot2)
library(dplyr)
library(igraph)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ------------------------------------------------------------------------
library(graydon.package)

## ------------------------------------------------------------------------
data.frame(`Column names` = names(tbl_SBI_count)) %>% 
  knitr::kable()

## ---- message=FALSE, warning=FALSE---------------------------------------
graph_SBI <- create_economic_activity_graph(tbl_SBI_count, 
                                            col_id = "code_SBI", 
                                            col_id_parent = "code_SBI_parent")

## ---- fig.height=6, fig.width=6------------------------------------------
plot_graydon_graph(graph_SBI, 
                   vertex.label = "", 
                   vertex.size = 3, 
                   edge.arrow.size = 0)

## ---- message=FALSE, warning=FALSE---------------------------------------
graph_SBI_rolled <- roll_up_hierarchy_by_minimum(graph_tree = graph_SBI,
                                                 name_attribute = "qty_companies",
                                                 name_propagated = "qty_companies_cum",
                                                 threshold = 5000)

## ---- fig.height=6, fig.width=6, warning=FALSE---------------------------
V(graph_SBI_rolled)$color <- ifelse(V(graph_SBI_rolled)$is_root, 1, 2)
V(graph_SBI_rolled)$label <- ifelse(V(graph_SBI_rolled)$is_root, V(graph_SBI_rolled)$name, "")

plot_graydon_graph(graph_SBI_rolled,
                   vertex.size = 3,
                   edge.arrow.size = 0)

## ---- fig.height=6, fig.width=6------------------------------------------
V(graph_SBI_rolled)$label <- format_number(V(graph_SBI_rolled)$qty_companies_cum)
V(graph_SBI_rolled)$label <- ifelse(V(graph_SBI_rolled)$label == 0, "", V(graph_SBI_rolled)$label)

plot_graydon_graph(graph_SBI_rolled,
                   vertex.size = 3,
                   edge.arrow.size = 0)

## ------------------------------------------------------------------------
list_graphs <- decompose(graph_SBI_rolled)

# Getting all ending activity codes
idx_searched <- names(sapply(list_graphs, function(x) igraph::V(x)[1] ))

plot_graydon_graph(list_graphs[[1]])

## ------------------------------------------------------------------------
df_translation_codes <- rolled_up_as_data_frame(graph_SBI_rolled)

## ---- echo=FALSE---------------------------------------------------------
df_translation_codes %>% 
  head() %>% 
  knitr::kable()

## ------------------------------------------------------------------------
tbl_NACE_recoded <- hierarchy_get_higher_level(tbl_NACE, level_no = 2,
                                               col_code= "code_NACE", col_code_parent = "code_NACE_parent", 
                                               col_layer_no = "hierarchy_layer")

tbl_NACE_recoded %>% 
  head(10) %>% 
  knitr::kable()
mark-me/graydon.package documentation built on Nov. 14, 2023, 5:31 p.m.