---
title: "Types de datasets et types de données"
author: "Antoine Neuraz"
date: "10/30/2019"
output: 
  bookdown::html_document2:
    code_folding: hide
---

# Types de datasets et types de données

```{r, include = FALSE}
library(ggplot2)
library(ggraph)
library(vizoR)
library(tidygraph)
library(igraph)
library(patchwork)
```


## Types de datasets

### Données tabulaires

```{r}


make_tabular_data <- function(x_size, y_size, highlight) {
  
  dt <- data.frame(x     = rep(1:x_size, y_size), 
                   y     = unlist(lapply(1:y_size, function(x) rep(x, x_size))),
                   group = rep(1, x_size*y_size))
  
  dt$group[highlight] <- 2
  
  dt
  
}

ggplot(data = make_tabular_data(5,3,8), 
       mapping = aes(x = x, y = y, fill = as.factor(group))) +
  geom_tile(color = 'white', size = 2)  +  
  annotate("segment", x=0.2,xend=5,y=3.8,yend=3.8,arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  annotate("segment", x=0.2,xend=0.2,y=3.8,yend=1,arrow=arrow(type="closed",length= unit(.4,"cm"))) + 
  annotate("text", x=3, y=0, label = "Cellule contenant une valeur",hjust=0.5) +
  annotate("segment", x=3,xend=3,y=0.2,yend=1.5,arrow=arrow(type="closed", length= unit(.4,"cm"))) +
  scale_fill_manual(values = list("lightgrey",  "darkred"))+
  scale_x_continuous(position = "top")+
 xlab('Attributs (colonnes)') + 
  ylab('Items (lignes)') +
  theme_elegant() +
  theme(legend.position = 'none',
        axis.text=element_blank(),
        axis.line =element_blank()) 

```

### Réseaux

```{r}


nodes <- data.frame(id = glue::glue("node{1:11}"), 
                    #label = c("node1","node2","node3"), 
                    node_type= rep(1,11))

edges <- data.frame(from = c("node1","node1","node1","node1","node1", "node5", "node5", "node5", "node6", "node9","node9"),
                    to = c("node2","node3", "node4", "node5","node6", "node7", "node8", "node9", "node9", "node10", "node11"),
                    type = rep("normal",11) ,
                    weight = rep(1,11))

net <- graph_from_data_frame(d=edges, vertices = nodes, directed = FALSE)

ggraph(net) + 
  geom_edge_link(end_cap = circle(0, 'mm'),
                 start_cap = circle(0, 'mm'), edge_width = 1) + 
  geom_node_point(color = 'darkred', fill ="darkred", size = 10, shape = 21) + 
   expand_limits(y=c(-1.5,1.5), x = c(-1.5,1.5)) +
  annotate("curve", x=1.2,xend=1.3,y=-.6,yend=-.3,curvature=.2, arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  annotate("text", x=1.2, y=-.6, label = "Noeud (Node)",hjust=0.5, vjust=1) +
  annotate("curve", x=.4,xend=.5,y=1,yend=.5,curvature=-0.2, arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  annotate("text", x=.4, y=1, label = "Lien (Edge)",hjust=0.5, vjust=-.7) +
  theme_void_complete()

```

### TODO: Champs

```{r}

make_grid_data <- function(size) {
  data.frame(x    = c(rep(1,size), 1:size), 
             xend = c(rep(size,size), 1:size), 
             y    = c(1:size, rep(1,size)), 
             yend = c(1:size, rep(size,size)))
}

size = 8

p1 <- ggplot(make_grid_data(size)) + 
  geom_segment(aes(x=x, xend=xend, y=y, yend=yend)) + 
  geom_rect(xmin = 2, xmax=3, ymin=6, ymax=7, fill="darkred") +
  annotate("text", x=4.5,y = 10, label= "Grille de positions", size =6)+
  xlim(c(1,size*2-1))+
  ylim(c(-5,size+2))+
  coord_polar(start = -pi/2) + 
  theme_void_complete() +
  theme(plot.margin=unit(c(0,0,0,0),"lines"),
        panel.spacing=unit(0,"lines"),
        axis.ticks.margin=unit(0,"cm"))
  

p2<- ggplot(data = make_tabular_data(5,1,3), 
       mapping = aes(x = x, y = y, fill = as.factor(group))) +
  geom_tile(color = 'white', size = 2)  +  
  annotate("segment", x=.7,xend=5.5,y=1.6,yend=1.6,arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  #annotate("segment", x=0.2,xend=0.2,y=3.8,yend=1,arrow=arrow(type="closed",length= unit(.4,"cm"))) + 
  annotate("text", x=3, y=0, label = "Valeur dans une cellule",hjust=0.5, vjust=1) +
  annotate("text", x=3, y=1.6, label = "Attributs (colonnes)",hjust=0.5, vjust=-1) +
  annotate("segment", x=3,xend=3,y=0,yend=0.5,arrow=arrow(type="closed", length= unit(.4,"cm"))) +
  scale_fill_manual(values = list("lightgrey",  "darkred"))+
  scale_x_continuous(position = "top")+
  ylim(-1, 2) +
  theme_void_complete() 


(plot_spacer()  | p1 | plot_spacer()) / (plot_spacer()  | p2 | plot_spacer()) 

```


### TODO: Spacial

## Types de données 

### TODO