---
title: "Perception : système visuel, marques et canaux, couleurs"
author: "Antoine Neuraz"
date: "10/30/2019"
output: 
  bookdown::html_document2:
    code_folding: hide
---

```{r, include=FALSE}
library(ggplot2)
library(tidyverse)
library(see) 
library(patchwork)

library(vizoR)

seed = 44

```

# Perception : système visuel, marques et canaux, couleurs 

## TODO

## Types de marques

```{r}

size <- list(100, 2)
min_x <- 0
max_x <- 1


p_color <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      color = group
    )
  ) +
  geom_point(size = 3) +
  scale_color_material_d() +
  theme_void_complete() +
  labs(subtitle= "Couleur")

#p_color
```

```{r}
p_angle <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  mutate(angle = ifelse(group == "group1", 0, pi / 3)) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      angle = angle
    )
  ) +
  geom_spoke(
    radius = 0.04,
    size = .8
  ) +
  theme_void_complete() +
  scale_color_material_d() +
  ggtitle("Angle")
#p_angle
```

```{r}
p_size <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  mutate(size = ifelse(group == "group1", 2, 3)) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      size = size
    )
  ) +
  geom_point() +
  theme_void_complete() +
  scale_size(range = c(1, 3)) +
  ggtitle("Taille")
#p_size
```


```{r}
p_grey <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      color = group
    )
  ) +
  geom_point(size = 3) +
  theme_void_complete() +
  scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) +
  ggtitle("Luminosité")
#p_grey
```

```{r}
dt <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  mutate(curvature = ifelse(group == "group1", 0, 1))

p_curve <- dt %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      xend = x, 
      yend = y+max_x/10,
      curvature = curvature
    )
  ) +
  geom_curve(data = subset(dt, group == 'group1'), curvature = 0) +
  geom_curve(data = subset(dt, group == 'group2'), curvature = .7) +
  scale_color_material_d() +
  theme_void_complete() +
  ggtitle("Courbe")
#p_curve
```

```{r}
dt <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) 

p_box <- dt %>%
  ggplot(
    data = .,
    aes(
      x = x,
      xend = x+max_x/50,
      y = y,
      yend = y
      
    )
  ) +
  geom_point(data = subset(dt, group=='group2'),aes(x = x+max_x/100), shape = 22, size = 4) + 
  geom_segment() +
  #geom_curve(data = subset(dt, group == 'group2'), curvature = .7) +
  scale_color_material_d() +
  theme_void_complete() +
  ggtitle("Encapsulage")
#p_box
```

```{r}
p_shape <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      shape = group
    )
  ) +
  geom_point(size = 3) +
  #scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) +
  theme_void_complete() +
  ggtitle("Forme")
#p_shape
```

```{r}
p_fill <- generate_dataset_uniform(
  dataset_size = size,
  min_x = min_x,
  max_x = max_x,
  seed = seed
) %>%
  ggplot(
    data = .,
    aes(
      x = x,
      y = y,
      fill = group
    )
  ) +
  geom_point(size = 3, shape = 21) +
  scale_fill_manual(values = c('group2' = 'black', 'group1' = 'white')) +
  theme_void_complete() +
  ggtitle("Remplissage")
#p_fill
```

```{r encode, fig.height=12, fig.cap='Exemples d\'encodage', fig.align='center'}
p_color + p_angle + 
  p_size + p_grey + 
  p_curve + p_box + 
  p_shape + p_fill + 
  plot_layout(ncol = 2) 


```

## Mappings in ggplot

```{r}

#theme_set(theme_void_real())

aes_pos <- ggplot() + 
  geom_segment(data = data.frame(x = c(0, 0.5),
                                 xend = c(1, 0.5),
                                 y = c(0.5, 0),
                                 yend = c(0.5, 1)),
                aes(x = x, y = y, xend = xend, yend = yend),
                arrow = arrow(length = grid::unit(12, "pt")), size = .75) +
  annotate("text", .5, 1, size = 8, vjust = 1, hjust = 2.5, label = "y") +
  annotate("text", 1, .5, size = 8, vjust = 2, hjust = 1, label = "x") + 
  theme_void() + theme(legend.position = 'none') +
  coord_cartesian(xlim = c(-.2, 1.2), ylim = c(-.2, 1.2)) +
  labs(title = 'position')


aes_color <- ggplot() +
  geom_tile(data = data.frame(x = 0.15 + .2333*(0:3)),
            aes(x, y = .5, fill = factor(x)), width = .2, height = .6) + 
  theme_void() + theme(legend.position = 'none') +
  scale_fill_viridis_d()  +
  labs(title = 'couleur')


aes_shape <- ggplot() +
  geom_point(data = data.frame(x = (.5 + 0:3)/4),
             aes(x, y = .5, shape = factor(x)), size = 8, fill = "grey80") +
  theme_void() + theme(legend.position = 'none') +
  scale_shape_manual(values = 21:24)  +
  expand_limits(x=c(0,1)) +
  labs(title = 'forme')


aes_size <- ggplot() +
  geom_point(data = data.frame(x = (.5 + 0:3)/4),
             aes(x, y = .5, size = factor(x)), shape = 21, fill = "grey80") +
  theme_void() + theme(legend.position = 'none') +
  scale_size_manual(values = c(2, 5, 8, 11))  +
   expand_limits(x=c(0,1)) +
  labs(title = 'taille')



aes_lwd <- ggplot() +
  geom_segment(data = data.frame(x = rep(0.05, 4),
                                 xend = rep(0.95, 4),
                                 y = (1.5 + 0:3)/6,
                                 yend = (1.5 + 0:3)/6,
                                 size = 4:1),
               aes(x = x, y = y, xend = xend, yend = yend, size = size)) +
  theme_void() + theme(legend.position = 'none') +
  scale_size_identity()  +
  labs(title = 'épaisseur de ligne')

aes_ltp <- ggplot() +
  geom_segment(data = data.frame(x = rep(0.05, 4),
                                 xend = rep(0.95, 4),
                                 y = (1.5 + 0:3)/6,
                                 yend = (1.5 + 0:3)/6,
                                 linetype = 4:1),
               aes(x = x, y = y, xend = xend, yend = yend, linetype = linetype), size = 1) +
  theme_void() + theme(legend.position = 'none') +
  scale_linetype_identity()  +
  labs(title = 'type de ligne')


aes_pos + aes_shape + aes_size + aes_color   + aes_lwd + aes_ltp +plot_layout(nrow=2)
```

figure from [https://serialmentor.com/dataviz/aesthetic-mapping.html]()

## scales in ggplot


```{r, fig.asp = .2}
df <- data.frame(x = c(1:4))
scale_num <- ggplot(df, aes(x)) + 
  geom_point(size = 3, color = "#0072B2", y = 1) + 
  scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "position  ") +
  scale_x_continuous(limits = c(.7, 4.4), breaks = 1:5, labels = c("1", "2", "3", "4", "5"), name = NULL, position = "top") +
  theme_minimal_grid() +
  theme(axis.ticks.length = grid::unit(0, "pt"),
        axis.text = element_text(size = 14),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank())
scale_color <- ggplot(df, aes(x, color = factor(x), fill = factor(x))) + 
  geom_point(size = 5, shape = 22, y = 1) + 
  scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "color  ") +
  scale_x_continuous(limits = c(.7, 4.4), breaks = NULL) +
  scale_color_manual(values = c("#0082A6", "#4EBBB9", "#9CDFC2", "#D8F0CD"), guide = "none") +
  scale_fill_manual(values = c("#0082A6", "#4EBBB9", "#9CDFC2", "#D8F0CD"), guide = "none") +
   theme_minimal() + 
  theme(axis.ticks.length = grid::unit(0, "pt"),
        axis.text.x = element_blank(),
        axis.text.y = element_text(size = 14),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank()) 
scale_shape <- ggplot(df, aes(x, shape = factor(x))) + 
  geom_point(size = 4, color = "grey30", y = 1, fill = "grey80") + 
  scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "shape  ") +
  scale_x_continuous(limits = c(.7, 4.4), breaks = NULL) +
  scale_shape_manual(values = 21:24, guide = "none") +
   theme_minimal() +
  theme(axis.ticks.length = grid::unit(0, "pt"),
        axis.text.x = element_blank(),
        axis.text.y = element_text(size = 14),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank()) 
scale_num + scale_shape + scale_color + plot_layout(ncol = 1)
```

figure from [https://serialmentor.com/dataviz/aesthetic-mapping.html]()
