diff --git a/courses/img/palettes.jpg b/courses/img/palettes.jpg new file mode 100644 index 0000000..c768426 Binary files /dev/null and b/courses/img/palettes.jpg differ diff --git a/courses/lab02-perception-colors.Rmd b/courses/lab02-perception-colors.Rmd index 1962897..6888c21 100644 --- a/courses/lab02-perception-colors.Rmd +++ b/courses/lab02-perception-colors.Rmd @@ -15,34 +15,269 @@ output: --- ```{r setup, include=FALSE} -knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(echo = TRUE, fig.asp = .6) library(ggplot2) -library(showtext) +#library(showtext) +library(vizoR) +library(dplyr) +library(patchwork) +library(see) +library(RColorBrewer) ``` --- -## Les couleurs dans ggplot2 +class: inverse, center, middle +# Perception des différentes marques dans ggplot2 +--- +## TODO: échauffement + +#### Générer un dataset aléatoire avec la fonction vizoR::generate_dataset_uniform +```{r} +size <- list(100, 2) +min_x <- 0 +max_x <- 1 +seed <- 34 +``` +--- +## TODO: perception + +#### 1. Réaliser des plots avec les échelles suivantes sur la variable group : +- couleur +- forme +- angle +- taille +- luminosité +- courbe +- encapsulage +- remplissage + +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. + +#### 2. Comparer l'efficacité des différentes échelles pour distinguer les 2 groupes + +--- +## Couleur + + +```{r, echo = F} + +dt <- generate_dataset_uniform( + dataset_size = size, + min_x = min_x, + max_x = max_x, + seed = seed +) +``` + +-- +.small[ +```{r, echo = T} + +p_color <- ggplot( data = dt, + aes(x = x, y = y, color = group)) + + geom_point(size = 3, alpha = .6) + + see::scale_color_material_d() + + vizoR::theme_void_complete() + + labs(subtitle= "Couleur") + +p_color +``` +] + + +--- +## Angle + +-- + +.small[ +```{r} +p_angle <- dt %>% + mutate(angle = ifelse(group == "group1", 0, pi / 3)) %>% + ggplot( + data = ., aes( x = x, y = y, angle = angle ) ) + + geom_spoke(radius = 0.02,size = .8, alpha = .6 ) + + theme_void_complete() + + scale_color_material_d() + + ggtitle("Angle") +p_angle +``` +] +--- +## Taille + +-- + +.small[ +```{r} +p_size <- dt %>% + mutate(size = ifelse(group == "group1", 2, 3)) %>% + ggplot( data = ., + aes( x = x,y = y,size = size)) + + geom_point(alpha = .6) + + theme_void_complete() + + scale_size(range = c(1, 3)) + + ggtitle("Taille") +p_size +``` +] + +--- +## Luminosité + +-- + +.small[ +```{r} +p_grey <- dt %>% + ggplot( + data = .,aes(x = x,y = y,color = group)) + + geom_point(size = 3, alpha = .6) + + theme_void_complete() + + #scale_color_grey() + + scale_color_grey(start=.8, end=.2)+ + ggtitle("Luminosité") +p_grey +``` +] +--- +## Courbe + +-- + +.small[ +```{r} +dt <- dt %>% + mutate(curvature = ifelse(group == "group1", 0, 1)) + +p_curve <- dt %>% + ggplot(data = .,aes(x = x,y = y,xend = x, yend = y+max_x/50, curvature = curvature)) + + #geom_curve()+ + geom_curve(data = subset(dt, group == 'group1'), curvature = 0, alpha = .7) + + geom_curve(data = subset(dt, group == 'group2'), curvature = .7, alpha = .7) + + scale_color_material_d() + + theme_void_complete() + + ggtitle("Courbe") +p_curve +``` +] +--- +## Encapsulage + +-- + +.small[ +```{r} +p_box <- dt %>% + ggplot(data = .,aes(x = x,xend = x+max_x/50,y = y,yend = y, group = group)) + + geom_point(data = subset(dt, group=='group2'),aes(x = x+max_x/100), shape = 22, size = 13) + + geom_segment() + + scale_color_material_d() + + theme_void_complete() + + ggtitle("Encapsulage") +p_box +``` +] + +--- +## Forme + +-- + +.small[ +```{r} +p_shape <- dt %>% + ggplot(data = .,aes(x = x,y = y,shape = group)) + + geom_point(size = 3, alpha = .6) + + theme_void_complete() + + ggtitle("Forme") +p_shape +``` +] +--- +## Remplissage + +-- + +.small[ +```{r} +p_fill <- dt %>% + ggplot(data = ., aes( x = x,y = y,fill = group)) + + geom_point(size = 3, shape = 21, alpha = .7) + + scale_fill_manual(values = c('group2' = 'black', 'group1' = 'white')) + + theme_void_complete() + + ggtitle("Remplissage") +p_fill +``` +] + +--- +class: full, center, middle +```{r, echo = F, out.width = '2000px'} +p_color + p_angle + + p_size + p_grey + + p_curve + p_box + + p_shape + p_fill + + plot_layout(ncol = 2) + + +``` + + +--- +class: full +### Les couleurs dans ggplot2 `display.brewer.all()` + +![:scale 80%](img/palettes.jpg) + +--- +## TODO: couleurs + +#### Charger le dataset diamonds et créer un sous-dataset aléatoire de 1000 lignes + +#### Plot carat en fonction du prix et de la couleur + +#### changer la palette par défaut vers une autre palette disponible + +--- +## TODO: couleurs 2 + +#### Plot carat en fonction du prix avec carat en double encodage + +#### Aller sur [http://colorbrewer2.org]() et trouver une palette divergente + +#### Créer une palette custom basée sur cette palette et l'appliquer au plot précédent + +#### Caler la palette sur le carat moyen + +#### Annoter le plot avec une ligne désignant le carat moyen et un texte expliquant cette ligne + + + +--- ```{r} dsamp <- diamonds[sample(nrow(diamonds), 1000), ] ggplot(dsamp, aes(carat, price)) + - geom_point(aes(colour = clarity)) + - scale_color_brewer(palette = "Set3") + geom_point(aes(colour = color)) + + scale_color_brewer(palette = "Set3") + + facet_wrap(~color) ``` ```{r} ggplot(dsamp, aes(carat, price)) + - geom_point(aes(colour = carat))) + + geom_point(aes(colour = carat)) + scale_color_distiller(palette="RdYlBu") ``` ```{r} -font_add_google("Gochi Hand", "gochi") -showtext_auto() +#showtext_auto() +#font_add_google("Schoolbell", "bell") + +font_family = "sans" annotate_color = "grey50" -font_add_google("Schoolbell", "bell") midpoint = (max(dsamp$carat)-min(dsamp$carat))/2 @@ -56,18 +291,22 @@ ggplot(dsamp, aes(carat, price)) + annotate("text", x=.78, y=15000, hjust=1, srt=40, label ="this is the midpoint", - family="bell", + family=font_family, color=annotate_color) + annotate("curve", x = .8, xend=midpoint-.01, y=15000, yend = 14000, curvature = -.5, color=annotate_color , arrow=arrow(length = unit(0.03, "npc") )) + - theme_minimal() + - theme(panel.grid.minor = element_blank(), - panel.grid.major.x = element_blank()) + ggthemes::theme_tufte() + theme_elegant() + + theme(panel.grid.minor = element_blank(), + panel.grid.major.x = element_blank(), + legend.position = "none") ``` + + + diff --git a/courses/lab02-perception-colors.html b/courses/lab02-perception-colors.html new file mode 100644 index 0000000..8b8f082 --- /dev/null +++ b/courses/lab02-perception-colors.html @@ -0,0 +1,401 @@ + + +
+library(tidyverse)
+## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
+## ✔ ggplot2 3.2.1 ✔ purrr 0.3.3
+## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
+## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
+## ✔ readr 1.3.1 ✔ forcats 0.4.0
+## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
+## ✖ dplyr::filter() masks stats::filter()
+## ✖ dplyr::lag() masks stats::lag()
+library(lubridate)
+##
+## Attaching package: 'lubridate'
+## The following object is masked from 'package:base':
+##
+## date
+library(ggmap)
+## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
+## Please cite ggmap if you use it! See citation("ggmap") for details.
+library(ggrepel)
+library(gridExtra)
+##
+## Attaching package: 'gridExtra'
+## The following object is masked from 'package:dplyr':
+##
+## combine
+library(pander)
+library(ggmap)
+
+# https://www.andrewheiss.com/blog/2017/08/10/exploring-minards-1812-plot-with-ggplot2/
+
+cities <- read.table("data/minard/cities.txt",
+ header = TRUE, stringsAsFactors = FALSE)
+
+troops <- read.table("data/minard/troops.txt",
+ header = TRUE, stringsAsFactors = FALSE)
+
+temps <- read.table("data/minard/temps.txt",
+ header = TRUE, stringsAsFactors = FALSE) %>%
+ mutate(date = dmy(date)) # Convert string to actual date
+
+temps.nice <- temps %>%
+ mutate(nice.label = paste0(temp, "°, ", month, ". ", day))
+font_family = "Helvetica"
+color_cities = "black" #"#DC5B44"
+
+color_troops_fw = "#DFC17E"
+color_troops_bw = "#252523"
+# No map this time
+march.1812.plot.simple <- ggplot() +
+ geom_path(data = troops, aes(x = long, y = lat, group = group,
+ color = direction, size = survivors),
+ lineend = "round") +
+ geom_point(data = cities, aes(x = long, y = lat),
+ color = color_cities) +
+ geom_text_repel(data = cities, aes(x = long, y = lat, label = city),
+ color = color_cities, family = font_family) +
+ scale_size(range = c(0.5, 15)) +
+ scale_colour_manual(values = c(color_troops_fw,color_troops_bw )) +
+ guides(color = FALSE, size = FALSE) +
+ theme_nothing()
+
+# Change the x-axis limits to match the simple map
+temps.1812.plot <- ggplot(data = temps.nice, aes(x = long, y = temp)) +
+ geom_line() +
+ geom_text_repel(aes(label = nice.label),
+ family = font_family, size = 2.5) +
+ labs(x = NULL, y = "° Celsius") +
+ scale_x_continuous(limits = ggplot_build(march.1812.plot.simple)$layout$panel_ranges[[1]]$x.range) +
+ scale_y_continuous(position = "right") +
+ coord_cartesian(ylim = c(-35, 5)) + # Add some space above/below
+ theme_bw(base_family = font_family) +
+ theme(panel.grid.major.x = element_blank(),
+ panel.grid.minor.x = element_blank(),
+ panel.grid.minor.y = element_blank(),
+ axis.text.x = element_blank(), axis.ticks = element_blank(),
+ panel.border = element_blank())
+
+# Combine the two plots
+both.1812.plot.simple <- rbind(ggplotGrob(march.1812.plot.simple),
+ ggplotGrob(temps.1812.plot))
+
+# Adjust panels
+panels <- both.1812.plot.simple$layout$t[grep("panel", both.1812.plot.simple$layout$name)]
+
+# Because this plot doesn't use coord_equal, since it's not a map, we can use whatever relative numbers we want, like a 3:1 ratio
+both.1812.plot.simple$heights[panels] <- unit(c(3, 1), "null")
+
+grid::grid.newpage()
+grid::grid.draw(both.1812.plot.simple)
+
+march.1812.europe <- c(left = -13.10, bottom = 35.75, right = 41.04, top = 61.86)
+
+march.1812.europe.map.wc <- get_stamenmap(bbox = march.1812.europe, zoom = 5,
+ maptype = "toner", where = "cache")
+## Source : http://tile.stamen.com/toner/5/14/8.png
+## Source : http://tile.stamen.com/toner/5/15/8.png
+## Source : http://tile.stamen.com/toner/5/16/8.png
+## Source : http://tile.stamen.com/toner/5/17/8.png
+## Source : http://tile.stamen.com/toner/5/18/8.png
+## Source : http://tile.stamen.com/toner/5/19/8.png
+## Source : http://tile.stamen.com/toner/5/14/9.png
+## Source : http://tile.stamen.com/toner/5/15/9.png
+## Source : http://tile.stamen.com/toner/5/16/9.png
+## Source : http://tile.stamen.com/toner/5/17/9.png
+## Source : http://tile.stamen.com/toner/5/18/9.png
+## Source : http://tile.stamen.com/toner/5/19/9.png
+## Source : http://tile.stamen.com/toner/5/14/10.png
+## Source : http://tile.stamen.com/toner/5/15/10.png
+## Source : http://tile.stamen.com/toner/5/16/10.png
+## Source : http://tile.stamen.com/toner/5/17/10.png
+## Source : http://tile.stamen.com/toner/5/18/10.png
+## Source : http://tile.stamen.com/toner/5/19/10.png
+## Source : http://tile.stamen.com/toner/5/14/11.png
+## Source : http://tile.stamen.com/toner/5/15/11.png
+## Source : http://tile.stamen.com/toner/5/16/11.png
+## Source : http://tile.stamen.com/toner/5/17/11.png
+## Source : http://tile.stamen.com/toner/5/18/11.png
+## Source : http://tile.stamen.com/toner/5/19/11.png
+## Source : http://tile.stamen.com/toner/5/14/12.png
+## Source : http://tile.stamen.com/toner/5/15/12.png
+## Source : http://tile.stamen.com/toner/5/16/12.png
+## Source : http://tile.stamen.com/toner/5/17/12.png
+## Source : http://tile.stamen.com/toner/5/18/12.png
+## Source : http://tile.stamen.com/toner/5/19/12.png
+ggmap(march.1812.europe.map.wc) +
+ geom_path(data = troops, aes(x = long, y = lat, group = group,
+ color = direction, size = survivors),
+ lineend = "round") +
+ scale_size(range = c(0.5, 5)) +
+ scale_colour_manual(values = c("#DFC17E", "#252523")) +
+ guides(color = FALSE, size = FALSE) +
+ theme_nothing() # This is a special theme that comes with ggmap
+
+
+
+
+
+