選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

409 行
10.0KB

  1. ---
  2. title: "Chapitre 1"
  3. author: "Antoine Neuraz"
  4. date: "5/28/2019"
  5. output: html_document
  6. ---
  7. # Intro
  8. ```{r , include=FALSE}
  9. knitr::opts_chunk$set(echo = FALSE)
  10. ```
  11. ```{r, include=FALSE}
  12. library(ggplot2)
  13. library(tidyverse)
  14. library(see)
  15. library(patchwork)
  16. source('R/util_functions.R')
  17. seed = 44
  18. my_theme_void <- function(base_size = 11,
  19. base_family = "",
  20. base_line_size = base_size / 170,
  21. base_rect_size = base_size / 170) {
  22. theme_void(base_size = base_size,
  23. base_family = base_family,
  24. base_line_size = base_line_size) %+replace%
  25. theme( legend.position = "none",
  26. plot.title = element_text(hjust = 0.5)
  27. )
  28. }
  29. ```
  30. ```{r}
  31. size <- 100
  32. min_x <- 0
  33. max_x <- 1
  34. outliers <- 2
  35. p_color <- generate_uniform_dataset(
  36. dataset_size = size,
  37. min_x = min_x,
  38. max_x = max_x,
  39. outliers = outliers,
  40. seed = seed
  41. ) %>%
  42. ggplot(
  43. data = .,
  44. aes(
  45. x = x,
  46. y = y,
  47. color = group
  48. )
  49. ) +
  50. geom_point(size = 3) +
  51. scale_color_material_d() +
  52. my_theme_void() +
  53. labs(subtitle= "Couleur")
  54. #p_color
  55. ```
  56. ```{r}
  57. p_angle <- generate_uniform_dataset(
  58. dataset_size = size,
  59. min_x = min_x,
  60. max_x = max_x,
  61. outliers = outliers,
  62. seed = seed
  63. ) %>%
  64. mutate(angle = ifelse(group == "group1", 0, pi / 3)) %>%
  65. ggplot(
  66. data = .,
  67. aes(
  68. x = x,
  69. y = y,
  70. angle = angle
  71. )
  72. ) +
  73. geom_spoke(
  74. radius = 0.04,
  75. size = .8
  76. ) +
  77. my_theme_void() +
  78. scale_color_material_d() +
  79. ggtitle("Angle")
  80. #p_angle
  81. ```
  82. ```{r}
  83. p_size <- generate_uniform_dataset(
  84. dataset_size = size,
  85. min_x = min_x,
  86. max_x = max_x,
  87. outliers = outliers,
  88. seed = seed
  89. ) %>%
  90. mutate(size = ifelse(group == "group1", 2, 3)) %>%
  91. ggplot(
  92. data = .,
  93. aes(
  94. x = x,
  95. y = y,
  96. size = size
  97. )
  98. ) +
  99. geom_point() +
  100. my_theme_void() +
  101. scale_size(range = c(1, 3)) +
  102. ggtitle("Taille")
  103. #p_size
  104. ```
  105. ```{r}
  106. p_grey <- generate_uniform_dataset(
  107. dataset_size = size,
  108. min_x = min_x,
  109. max_x = max_x,
  110. outliers = outliers,
  111. seed = seed
  112. ) %>%
  113. ggplot(
  114. data = .,
  115. aes(
  116. x = x,
  117. y = y,
  118. color = group
  119. )
  120. ) +
  121. geom_point(size = 3) +
  122. my_theme_void() +
  123. scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) +
  124. ggtitle("Luminosité")
  125. #p_grey
  126. ```
  127. ```{r}
  128. dt <- generate_uniform_dataset(
  129. dataset_size = size,
  130. min_x = min_x,
  131. max_x = max_x,
  132. outliers = outliers,
  133. seed = seed
  134. ) %>%
  135. mutate(curvature = ifelse(group == "group1", 0, 1))
  136. p_curve <- dt %>%
  137. ggplot(
  138. data = .,
  139. aes(
  140. x = x,
  141. y = y,
  142. xend = x,
  143. yend = y+max_x/10,
  144. curvature = curvature
  145. )
  146. ) +
  147. geom_curve(data = subset(dt, group == 'group1'), curvature = 0) +
  148. geom_curve(data = subset(dt, group == 'group2'), curvature = .7) +
  149. scale_color_material_d() +
  150. my_theme_void() +
  151. ggtitle("Courbe")
  152. #p_curve
  153. ```
  154. ```{r}
  155. dt <- generate_uniform_dataset(
  156. dataset_size = size,
  157. min_x = min_x,
  158. max_x = max_x,
  159. outliers = outliers,
  160. seed = seed
  161. )
  162. p_box <- dt %>%
  163. ggplot(
  164. data = .,
  165. aes(
  166. x = x,
  167. xend = x+max_x/50,
  168. y = y,
  169. yend = y
  170. )
  171. ) +
  172. geom_point(data = subset(dt, group=='group2'),aes(x = x+max_x/100), shape = 22, size = 4) +
  173. geom_segment() +
  174. #geom_curve(data = subset(dt, group == 'group2'), curvature = .7) +
  175. scale_color_material_d() +
  176. my_theme_void() +
  177. ggtitle("Encapsulage")
  178. #p_box
  179. ```
  180. ```{r}
  181. p_shape <- generate_uniform_dataset(
  182. dataset_size = size,
  183. min_x = min_x,
  184. max_x = max_x,
  185. outliers = outliers,
  186. seed = seed
  187. ) %>%
  188. ggplot(
  189. data = .,
  190. aes(
  191. x = x,
  192. y = y,
  193. shape = group
  194. )
  195. ) +
  196. geom_point(size = 3) +
  197. #scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) +
  198. my_theme_void() +
  199. ggtitle("Forme")
  200. #p_shape
  201. ```
  202. ```{r}
  203. p_fill <- generate_uniform_dataset(
  204. dataset_size = size,
  205. min_x = min_x,
  206. max_x = max_x,
  207. outliers = outliers,
  208. seed = seed
  209. ) %>%
  210. ggplot(
  211. data = .,
  212. aes(
  213. x = x,
  214. y = y,
  215. fill = group
  216. )
  217. ) +
  218. geom_point(size = 3, shape = 21) +
  219. scale_fill_manual(values = c('group2' = 'black', 'group1' = 'white')) +
  220. my_theme_void() +
  221. ggtitle("Remplissage")
  222. #p_fill
  223. ```
  224. ```{r encode, fig.height=12, fig.cap='Exemples d\'encodage', fig.align='center'}
  225. p_color + p_angle +
  226. p_size + p_grey +
  227. p_curve + p_box +
  228. p_shape + p_fill +
  229. plot_layout(ncol = 2)
  230. ```
  231. ## Mappings in ggplot
  232. ```{r}
  233. #theme_set(theme_void_real())
  234. aes_pos <- ggplot() +
  235. geom_segment(data = data.frame(x = c(0, 0.5),
  236. xend = c(1, 0.5),
  237. y = c(0.5, 0),
  238. yend = c(0.5, 1)),
  239. aes(x = x, y = y, xend = xend, yend = yend),
  240. arrow = arrow(length = grid::unit(12, "pt")), size = .75) +
  241. annotate("text", .5, 1, size = 8, vjust = 1, hjust = 2.5, label = "y") +
  242. annotate("text", 1, .5, size = 8, vjust = 2, hjust = 1, label = "x") +
  243. theme_void() + theme(legend.position = 'none') +
  244. coord_cartesian(xlim = c(-.2, 1.2), ylim = c(-.2, 1.2)) +
  245. labs(title = 'position')
  246. aes_color <- ggplot() +
  247. geom_tile(data = data.frame(x = 0.15 + .2333*(0:3)),
  248. aes(x, y = .5, fill = factor(x)), width = .2, height = .6) +
  249. theme_void() + theme(legend.position = 'none') +
  250. scale_fill_viridis_d() +
  251. labs(title = 'couleur')
  252. aes_shape <- ggplot() +
  253. geom_point(data = data.frame(x = (.5 + 0:3)/4),
  254. aes(x, y = .5, shape = factor(x)), size = 8, fill = "grey80") +
  255. theme_void() + theme(legend.position = 'none') +
  256. scale_shape_manual(values = 21:24) +
  257. expand_limits(x=c(0,1)) +
  258. labs(title = 'forme')
  259. aes_size <- ggplot() +
  260. geom_point(data = data.frame(x = (.5 + 0:3)/4),
  261. aes(x, y = .5, size = factor(x)), shape = 21, fill = "grey80") +
  262. theme_void() + theme(legend.position = 'none') +
  263. scale_size_manual(values = c(2, 5, 8, 11)) +
  264. expand_limits(x=c(0,1)) +
  265. labs(title = 'taille')
  266. aes_lwd <- ggplot() +
  267. geom_segment(data = data.frame(x = rep(0.05, 4),
  268. xend = rep(0.95, 4),
  269. y = (1.5 + 0:3)/6,
  270. yend = (1.5 + 0:3)/6,
  271. size = 4:1),
  272. aes(x = x, y = y, xend = xend, yend = yend, size = size)) +
  273. theme_void() + theme(legend.position = 'none') +
  274. scale_size_identity() +
  275. labs(title = 'épaisseur de ligne')
  276. aes_ltp <- ggplot() +
  277. geom_segment(data = data.frame(x = rep(0.05, 4),
  278. xend = rep(0.95, 4),
  279. y = (1.5 + 0:3)/6,
  280. yend = (1.5 + 0:3)/6,
  281. linetype = 4:1),
  282. aes(x = x, y = y, xend = xend, yend = yend, linetype = linetype), size = 1) +
  283. theme_void() + theme(legend.position = 'none') +
  284. scale_linetype_identity() +
  285. labs(title = 'type de ligne')
  286. aes_pos + aes_shape + aes_size + aes_color + aes_lwd + aes_ltp +plot_layout(nrow=2)
  287. ```
  288. figure from [https://serialmentor.com/dataviz/aesthetic-mapping.html]()
  289. ## scales in ggplot
  290. ```{r, fig.asp = .2}
  291. df <- data.frame(x = c(1:4))
  292. scale_num <- ggplot(df, aes(x)) +
  293. geom_point(size = 3, color = "#0072B2", y = 1) +
  294. scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "position ") +
  295. scale_x_continuous(limits = c(.7, 4.4), breaks = 1:5, labels = c("1", "2", "3", "4", "5"), name = NULL, position = "top") +
  296. theme_minimal_grid() +
  297. theme(axis.ticks.length = grid::unit(0, "pt"),
  298. axis.text = element_text(size = 14),
  299. axis.title.y = element_blank(),
  300. axis.ticks.y = element_blank())
  301. scale_color <- ggplot(df, aes(x, color = factor(x), fill = factor(x))) +
  302. geom_point(size = 5, shape = 22, y = 1) +
  303. scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "color ") +
  304. scale_x_continuous(limits = c(.7, 4.4), breaks = NULL) +
  305. scale_color_manual(values = c("#0082A6", "#4EBBB9", "#9CDFC2", "#D8F0CD"), guide = "none") +
  306. scale_fill_manual(values = c("#0082A6", "#4EBBB9", "#9CDFC2", "#D8F0CD"), guide = "none") +
  307. theme_minimal() +
  308. theme(axis.ticks.length = grid::unit(0, "pt"),
  309. axis.text.x = element_blank(),
  310. axis.text.y = element_text(size = 14),
  311. axis.title = element_blank(),
  312. axis.ticks = element_blank(),
  313. panel.grid.major = element_blank())
  314. scale_shape <- ggplot(df, aes(x, shape = factor(x))) +
  315. geom_point(size = 4, color = "grey30", y = 1, fill = "grey80") +
  316. scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1, label = "shape ") +
  317. scale_x_continuous(limits = c(.7, 4.4), breaks = NULL) +
  318. scale_shape_manual(values = 21:24, guide = "none") +
  319. theme_minimal() +
  320. theme(axis.ticks.length = grid::unit(0, "pt"),
  321. axis.text.x = element_blank(),
  322. axis.text.y = element_text(size = 14),
  323. axis.title = element_blank(),
  324. axis.ticks = element_blank(),
  325. panel.grid.major = element_blank())
  326. scale_num + scale_shape + scale_color + plot_layout(ncol = 1)
  327. ```
  328. figure from [https://serialmentor.com/dataviz/aesthetic-mapping.html]()
  329. ```{r}
  330. dt <- data.frame(x = rep(1:5, 3),
  331. y = c(rep(1,5), rep(2,5),rep(3,5)),
  332. group = rep(1, 5*3))
  333. dt$group[8] <- 2
  334. ggplot(data = dt,
  335. mapping = aes(x = x, y = y, fill = as.factor(group))) +
  336. geom_tile(color = 'white', size = 2) +
  337. annotate("segment", x=0.2,xend=5,y=3.8,yend=3.8,arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  338. annotate("segment", x=0.2,xend=0.2,y=3.8,yend=1,arrow=arrow(type="closed",length= unit(.4,"cm"))) +
  339. annotate("text", x=3, y=0, label = "Cellule contenant une valeur",hjust=0.5) +
  340. annotate("segment", x=3,xend=3,y=0.2,yend=1.5,arrow=arrow(type="closed", length= unit(.4,"cm"))) +
  341. scale_fill_manual(values = list("lightgrey", "darkred"))+
  342. scale_x_continuous(position = "top")+
  343. xlab('Attributs (colonnes)') +
  344. ylab('Items (lignes)') +
  345. theme_modern() +
  346. theme(legend.position = 'none',
  347. axis.text=element_blank(),
  348. axis.line =element_blank())
  349. ```