Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

lab02-perception-colors.Rmd 6.0KB

5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. ---
  27. class: inverse, center, middle
  28. # Perception des différentes marques dans ggplot2
  29. ---
  30. ## TODO: échauffement
  31. #### Générer un dataset aléatoire avec la fonction vizoR::generate_dataset_uniform
  32. ```{r}
  33. size <- list(100, 2)
  34. min_x <- 0
  35. max_x <- 1
  36. seed <- 34
  37. ```
  38. ---
  39. ## TODO: perception
  40. #### 1. Réaliser des plots avec les échelles suivantes sur la variable group :
  41. - couleur
  42. - forme
  43. - angle
  44. - taille
  45. - luminosité
  46. - courbe
  47. - encapsulage
  48. - remplissage
  49. 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.
  50. #### 2. Comparer l'efficacité des différentes échelles pour distinguer les 2 groupes
  51. ---
  52. ## Couleur
  53. ```{r, echo = F}
  54. dt <- generate_dataset_uniform(
  55. dataset_size = size,
  56. min_x = min_x,
  57. max_x = max_x,
  58. seed = seed
  59. )
  60. ```
  61. --
  62. .small[
  63. ```{r, echo = T}
  64. p_color <- ggplot( data = dt,
  65. aes(x = x, y = y, color = group)) +
  66. geom_point(size = 3, alpha = .6) +
  67. see::scale_color_material_d() +
  68. vizoR::theme_void_complete() +
  69. labs(subtitle= "Couleur")
  70. p_color
  71. ```
  72. ]
  73. ---
  74. ## Angle
  75. --
  76. .small[
  77. ```{r}
  78. p_angle <- dt %>%
  79. mutate(angle = ifelse(group == "group1", 0, pi / 3)) %>%
  80. ggplot(
  81. data = ., aes( x = x, y = y, angle = angle ) ) +
  82. geom_spoke(radius = 0.02,size = .8, alpha = .6 ) +
  83. theme_void_complete() +
  84. scale_color_material_d() +
  85. ggtitle("Angle")
  86. p_angle
  87. ```
  88. ]
  89. ---
  90. ## Taille
  91. --
  92. .small[
  93. ```{r}
  94. p_size <- dt %>%
  95. mutate(size = ifelse(group == "group1", 2, 3)) %>%
  96. ggplot( data = .,
  97. aes( x = x,y = y,size = size)) +
  98. geom_point(alpha = .6) +
  99. theme_void_complete() +
  100. scale_size(range = c(1, 3)) +
  101. ggtitle("Taille")
  102. p_size
  103. ```
  104. ]
  105. ---
  106. ## Luminosité
  107. --
  108. .small[
  109. ```{r}
  110. p_grey <- dt %>%
  111. ggplot(
  112. data = .,aes(x = x,y = y,color = group)) +
  113. geom_point(size = 3, alpha = .6) +
  114. theme_void_complete() +
  115. #scale_color_grey() +
  116. scale_color_grey(start=.8, end=.2)+
  117. ggtitle("Luminosité")
  118. p_grey
  119. ```
  120. ]
  121. ---
  122. ## Courbe
  123. --
  124. .small[
  125. ```{r}
  126. dt <- dt %>%
  127. mutate(curvature = ifelse(group == "group1", 0, 1))
  128. p_curve <- dt %>%
  129. ggplot(data = .,aes(x = x,y = y,xend = x, yend = y+max_x/50, curvature = curvature)) +
  130. #geom_curve()+
  131. geom_curve(data = subset(dt, group == 'group1'), curvature = 0, alpha = .7) +
  132. geom_curve(data = subset(dt, group == 'group2'), curvature = .7, alpha = .7) +
  133. scale_color_material_d() +
  134. theme_void_complete() +
  135. ggtitle("Courbe")
  136. p_curve
  137. ```
  138. ]
  139. ---
  140. ## Encapsulage
  141. --
  142. .small[
  143. ```{r}
  144. p_box <- dt %>%
  145. ggplot(data = .,aes(x = x,xend = x+max_x/50,y = y,yend = y, group = group)) +
  146. geom_point(data = subset(dt, group=='group2'),aes(x = x+max_x/100), shape = 22, size = 13) +
  147. geom_segment() +
  148. scale_color_material_d() +
  149. theme_void_complete() +
  150. ggtitle("Encapsulage")
  151. p_box
  152. ```
  153. ]
  154. ---
  155. ## Forme
  156. --
  157. .small[
  158. ```{r}
  159. p_shape <- dt %>%
  160. ggplot(data = .,aes(x = x,y = y,shape = group)) +
  161. geom_point(size = 3, alpha = .6) +
  162. theme_void_complete() +
  163. ggtitle("Forme")
  164. p_shape
  165. ```
  166. ]
  167. ---
  168. ## Remplissage
  169. --
  170. .small[
  171. ```{r}
  172. p_fill <- dt %>%
  173. ggplot(data = ., aes( x = x,y = y,fill = group)) +
  174. geom_point(size = 3, shape = 21, alpha = .7) +
  175. scale_fill_manual(values = c('group2' = 'black', 'group1' = 'white')) +
  176. theme_void_complete() +
  177. ggtitle("Remplissage")
  178. p_fill
  179. ```
  180. ]
  181. ---
  182. class: full, center, middle
  183. ```{r, echo = F, out.width = '2000px'}
  184. p_color + p_angle +
  185. p_size + p_grey +
  186. p_curve + p_box +
  187. p_shape + p_fill +
  188. plot_layout(ncol = 2)
  189. ```
  190. ---
  191. class: full
  192. ### Les couleurs dans ggplot2 `display.brewer.all()`
  193. ![:scale 80%](img/palettes.jpg)
  194. ---
  195. ## TODO: couleurs
  196. #### Charger le dataset diamonds et créer un sous-dataset aléatoire de 1000 lignes
  197. #### Plot carat en fonction du prix et de la couleur
  198. #### changer la palette par défaut vers une autre palette disponible
  199. ---
  200. ## TODO: couleurs 2
  201. #### Plot carat en fonction du prix avec carat en double encodage
  202. #### Aller sur [http://colorbrewer2.org]() et trouver une palette divergente
  203. #### Créer une palette custom basée sur cette palette et l'appliquer au plot précédent
  204. #### Caler la palette sur le carat moyen
  205. #### Annoter le plot avec une ligne désignant le carat moyen et un texte expliquant cette ligne
  206. ---
  207. ```{r}
  208. dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
  209. ggplot(dsamp, aes(carat, price)) +
  210. geom_point(aes(colour = color)) +
  211. scale_color_brewer(palette = "Set3") +
  212. facet_wrap(~color)
  213. ```
  214. ```{r}
  215. ggplot(dsamp, aes(carat, price)) +
  216. geom_point(aes(colour = carat)) +
  217. scale_color_distiller(palette="RdYlBu")
  218. ```
  219. ```{r}
  220. #showtext_auto()
  221. #font_add_google("Schoolbell", "bell")
  222. font_family = "sans"
  223. annotate_color = "grey50"
  224. midpoint = (max(dsamp$carat)-min(dsamp$carat))/2
  225. ggplot(dsamp, aes(carat, price)) +
  226. geom_vline(xintercept = midpoint, color = annotate_color) +
  227. geom_point(aes(colour = carat)) +
  228. scale_color_gradient2(low = "#d8b365",
  229. mid="#f5f5f5",
  230. high="#5ab4ac",
  231. midpoint = midpoint) +
  232. annotate("text",
  233. x=.78, y=15000, hjust=1, srt=40,
  234. label ="this is the midpoint",
  235. family=font_family,
  236. color=annotate_color) +
  237. annotate("curve",
  238. x = .8, xend=midpoint-.01, y=15000, yend = 14000,
  239. curvature = -.5,
  240. color=annotate_color ,
  241. arrow=arrow(length = unit(0.03, "npc") )) +
  242. theme_elegant() +
  243. theme(panel.grid.minor = element_blank(),
  244. panel.grid.major.x = element_blank(),
  245. legend.position = "none")
  246. ```