You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lab02-perception-colors.Rmd 7.2KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. ---
  2. title: "LAB 2: Perception et couleurs"
  3. author: "Antoine Neuraz"
  4. date: "19/11/2019"
  5. output:
  6. xaringan::moon_reader:
  7. css: ['default','css/my_style.css']
  8. lib_dir: libs
  9. seal: false
  10. nature:
  11. ratio: '4:3'
  12. countIncrementalSlides: false
  13. self-contained: true
  14. beforeInit: "addons/macros.js"
  15. ---
  16. ```{r setup, include=FALSE}
  17. knitr::opts_chunk$set(echo = TRUE, fig.asp = .6)
  18. library(ggplot2)
  19. #library(showtext)
  20. library(vizoR)
  21. library(dplyr)
  22. library(patchwork)
  23. library(see)
  24. library(RColorBrewer)
  25. ```
  26. class: center, middle, title
  27. # Lab 2: Perception et couleurs
  28. ### 2019-2020
  29. ## Dr. Antoine Neuraz
  30. ### AHU Informatique médicale
  31. #### Hôpital Necker-Enfants malades, </br> Université de Paris
  32. ---
  33. class: inverse, center, middle
  34. # Perception des différentes marques dans ggplot2
  35. ---
  36. ## TODO: échauffement
  37. #### Générer un dataset aléatoire avec la fonction vizoR::generate_dataset_uniform
  38. ```{r}
  39. size <- list(100, 2)
  40. min_x <- 0
  41. max_x <- 1
  42. seed <- 34
  43. ```
  44. ---
  45. ## TODO: perception
  46. #### 1. Réaliser des plots avec les échelles suivantes sur la variable group :
  47. - couleur
  48. - forme
  49. - angle
  50. - taille
  51. - luminosité
  52. - courbe
  53. - encapsulage
  54. - remplissage
  55. Certaines échelles sont très simples à mettre en place (e.g. couleur, forme) mais d'autres n'existent pas directement. Il faut trouver une alternative.
  56. #### 2. Comparer l'efficacité des différentes échelles pour distinguer les 2 groupes
  57. ---
  58. ## Couleur
  59. ```{r, echo = F}
  60. dt <- generate_dataset_uniform(
  61. dataset_size = size,
  62. min_x = min_x,
  63. max_x = max_x,
  64. seed = seed
  65. )
  66. ```
  67. --
  68. .small[
  69. ```{r, echo = T}
  70. p_color <- ggplot( data = dt,
  71. aes(x = x, y = y, color = group)) +
  72. geom_point(size = 3, alpha = .6) +
  73. see::scale_color_material_d() +
  74. vizoR::theme_void_complete() +
  75. labs(subtitle= "Couleur")
  76. p_color
  77. ```
  78. ]
  79. ---
  80. ## Angle
  81. --
  82. .small[
  83. ```{r}
  84. p_angle <- dt %>%
  85. mutate(angle = ifelse(group == "group1", 0, pi / 3)) %>%
  86. ggplot(
  87. data = ., aes( x = x, y = y, angle = angle ) ) +
  88. geom_spoke(radius = 0.02,size = .8, alpha = .6 ) +
  89. theme_void_complete() +
  90. scale_color_material_d() +
  91. ggtitle("Angle")
  92. p_angle
  93. ```
  94. ]
  95. ---
  96. ## Taille
  97. --
  98. .small[
  99. ```{r}
  100. p_size <- dt %>%
  101. mutate(size = ifelse(group == "group1", 2, 3)) %>%
  102. ggplot( data = .,
  103. aes( x = x,y = y,size = size)) +
  104. geom_point(alpha = .6) +
  105. theme_void_complete() +
  106. scale_size(range = c(1, 3)) +
  107. ggtitle("Taille")
  108. p_size
  109. ```
  110. ]
  111. ---
  112. ## Luminosité
  113. --
  114. .small[
  115. ```{r}
  116. p_grey <- dt %>%
  117. ggplot(
  118. data = .,aes(x = x,y = y,color = group)) +
  119. geom_point(size = 3, alpha = .6) +
  120. theme_void_complete() +
  121. #scale_color_grey() +
  122. scale_color_grey(start=.8, end=.2)+
  123. ggtitle("Luminosité")
  124. p_grey
  125. ```
  126. ]
  127. ---
  128. ## Courbe
  129. --
  130. .small[
  131. ```{r}
  132. dt <- dt %>%
  133. mutate(curvature = ifelse(group == "group1", 0, 1))
  134. p_curve <- dt %>%
  135. ggplot(data = .,aes(x = x,y = y,xend = x, yend = y+max_x/50, curvature = curvature)) +
  136. #geom_curve()+
  137. geom_curve(data = subset(dt, group == 'group1'), curvature = 0, alpha = .7) +
  138. geom_curve(data = subset(dt, group == 'group2'), curvature = .7, alpha = .7) +
  139. scale_color_material_d() +
  140. theme_void_complete() +
  141. ggtitle("Courbe")
  142. p_curve
  143. ```
  144. ]
  145. ---
  146. ## Encapsulage
  147. --
  148. .small[
  149. ```{r}
  150. p_box <- dt %>%
  151. ggplot(data = .,aes(x = x,xend = x+max_x/50,y = y,yend = y, group = group)) +
  152. geom_point(data = subset(dt, group=='group2'),aes(x = x+max_x/100), shape = 22, size = 13) +
  153. geom_segment() +
  154. scale_color_material_d() +
  155. theme_void_complete() +
  156. ggtitle("Encapsulage")
  157. p_box
  158. ```
  159. ]
  160. ---
  161. ## Forme
  162. --
  163. .small[
  164. ```{r}
  165. p_shape <- dt %>%
  166. ggplot(data = .,aes(x = x,y = y,shape = group)) +
  167. geom_point(size = 3, alpha = .6) +
  168. theme_void_complete() +
  169. ggtitle("Forme")
  170. p_shape
  171. ```
  172. ]
  173. ---
  174. ## Remplissage
  175. --
  176. .small[
  177. ```{r}
  178. p_fill <- dt %>%
  179. ggplot(data = ., aes( x = x,y = y,fill = group)) +
  180. geom_point(size = 3, shape = 21, alpha = .7) +
  181. scale_fill_manual(values = c('group2' = 'black', 'group1' = 'white')) +
  182. theme_void_complete() +
  183. ggtitle("Remplissage")
  184. p_fill
  185. ```
  186. ]
  187. ---
  188. class: full, center, middle
  189. ```{r, echo = F, out.width = '2000px'}
  190. p_color + p_angle +
  191. p_size + p_grey +
  192. p_curve + p_box +
  193. p_shape + p_fill +
  194. plot_layout(ncol = 2)
  195. ```
  196. ---
  197. class: full
  198. ### Les couleurs dans ggplot2 `display.brewer.all()`
  199. ![:scale 80%](img/palettes.jpg)
  200. ---
  201. ## TODO: couleurs
  202. #### Charger le dataset diamonds et créer un sous-dataset aléatoire de 1000 lignes
  203. #### Plot carat en fonction du prix et de la couleur
  204. #### changer la palette par défaut vers une autre palette disponible
  205. ---
  206. ```{r}
  207. dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  208. ggplot(dsamp, aes(carat, price)) +
  209. geom_point(aes(colour = color)) +
  210. scale_color_brewer(palette = "Set3") +
  211. facet_wrap(~color)
  212. ```
  213. ---
  214. ## TODO: couleurs 2
  215. #### Plot carat en fonction du prix avec carat en double encodage
  216. #### Aller sur [http://colorbrewer2.org]() et trouver une palette divergente
  217. #### Créer une palette custom basée sur cette palette et l'appliquer au plot précédent
  218. #### Caler la palette sur le carat moyen
  219. #### Annoter le plot avec une ligne désignant le carat moyen et un texte expliquant cette ligne
  220. ---
  221. ```{r}
  222. ggplot(dsamp, aes(carat, price)) +
  223. geom_point(aes(colour = carat)) +
  224. scale_color_distiller(palette="RdYlBu")
  225. ```
  226. ---
  227. ```{r, eval = F}
  228. #showtext_auto()
  229. #font_add_google("Schoolbell", "bell")
  230. font_family = "sans"
  231. annotate_color = "grey50"
  232. midpoint = (max(dsamp$carat)-min(dsamp$carat))/2
  233. ggplot(dsamp, aes(carat, price)) +
  234. geom_vline(xintercept = midpoint, color = annotate_color) +
  235. geom_point(aes(colour = carat)) +
  236. scale_color_gradient2(low = "#d8b365",
  237. mid="#f5f5f5",
  238. high="#5ab4ac",
  239. midpoint = midpoint) +
  240. annotate("text",
  241. x=.78, y=15000, hjust=1, srt=40,
  242. label ="this is the midpoint",
  243. family=font_family,
  244. color=annotate_color) +
  245. annotate("curve",
  246. x = .8, xend=midpoint-.01, y=15000, yend = 14000,
  247. curvature = -.5,
  248. color=annotate_color ,
  249. arrow=arrow(length = unit(0.03, "npc") )) +
  250. theme_elegant() +
  251. theme(panel.grid.minor = element_blank(),
  252. panel.grid.major.x = element_blank(),
  253. legend.position = "none")
  254. ```
  255. ---
  256. ```{r, echo = F}
  257. #showtext_auto()
  258. #font_add_google("Schoolbell", "bell")
  259. font_family = "sans"
  260. annotate_color = "grey50"
  261. midpoint = (max(dsamp$carat)-min(dsamp$carat))/2
  262. ggplot(dsamp, aes(carat, price)) +
  263. geom_vline(xintercept = midpoint, color = annotate_color) +
  264. geom_point(aes(colour = carat)) +
  265. scale_color_gradient2(low = "#d8b365",
  266. mid="#f5f5f5",
  267. high="#5ab4ac",
  268. midpoint = midpoint) +
  269. annotate("text",
  270. x=.78, y=15000, hjust=1, srt=40,
  271. label ="this is the midpoint",
  272. family=font_family,
  273. color=annotate_color) +
  274. annotate("curve",
  275. x = .8, xend=midpoint-.01, y=15000, yend = 14000,
  276. curvature = -.5,
  277. color=annotate_color ,
  278. arrow=arrow(length = unit(0.03, "npc") )) +
  279. theme_elegant() +
  280. theme(panel.grid.minor = element_blank(),
  281. panel.grid.major.x = element_blank(),
  282. legend.position = "none")
  283. ```