--- title: "Chapitre 1" author: "Antoine Neuraz" date: "5/28/2019" output: html_document --- # Intro ```{r , include=FALSE} knitr::opts_chunk$set(echo = FALSE) ``` ```{r, include=FALSE} library(ggplot2) library(tidyverse) library(see) library(patchwork) source('R/util_functions.R') seed = 44 my_theme_void <- function(base_size = 11, base_family = "", base_line_size = base_size / 170, base_rect_size = base_size / 170) { theme_void(base_size = base_size, base_family = base_family, base_line_size = base_line_size) %+replace% theme( legend.position = "none", plot.title = element_text(hjust = 0.5) ) } ``` ```{r} size <- 100 min_x <- 0 max_x <- 1 outliers <- 2 p_color <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, seed = seed ) %>% ggplot( data = ., aes( x = x, y = y, color = group ) ) + geom_point(size = 3) + scale_color_material_d() + my_theme_void() + labs(subtitle= "Couleur") #p_color ``` ```{r} p_angle <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, 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 ) + my_theme_void() + scale_color_material_d() + ggtitle("Angle") #p_angle ``` ```{r} p_size <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, seed = seed ) %>% mutate(size = ifelse(group == "group1", 2, 3)) %>% ggplot( data = ., aes( x = x, y = y, size = size ) ) + geom_point() + my_theme_void() + scale_size(range = c(1, 3)) + ggtitle("Taille") #p_size ``` ```{r} p_grey <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, seed = seed ) %>% ggplot( data = ., aes( x = x, y = y, color = group ) ) + geom_point(size = 3) + my_theme_void() + scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) + ggtitle("Luminosité") #p_grey ``` ```{r} dt <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, 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() + my_theme_void() + ggtitle("Courbe") #p_curve ``` ```{r} dt <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, 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() + my_theme_void() + ggtitle("Encapsulage") #p_box ``` ```{r} p_shape <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, seed = seed ) %>% ggplot( data = ., aes( x = x, y = y, shape = group ) ) + geom_point(size = 3) + #scale_color_manual(values = c('group2' = 'black', 'group1' = 'lightgrey')) + my_theme_void() + ggtitle("Forme") #p_shape ``` ```{r} p_fill <- generate_uniform_dataset( dataset_size = size, min_x = min_x, max_x = max_x, outliers = outliers, 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')) + my_theme_void() + 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]() ```{r} dt <- data.frame(x = rep(1:5, 3), y = c(rep(1,5), rep(2,5),rep(3,5)), group = rep(1, 5*3)) dt$group[8] <- 2 ggplot(data = dt, mapping = aes(x = x, y = y, fill = as.factor(group))) + geom_tile(color = 'white', size = 2) + annotate("segment", x=0.2,xend=5,y=3.8,yend=3.8,arrow=arrow(type="closed",length= unit(.4,"cm"))) + annotate("segment", x=0.2,xend=0.2,y=3.8,yend=1,arrow=arrow(type="closed",length= unit(.4,"cm"))) + annotate("text", x=3, y=0, label = "Cellule contenant une valeur",hjust=0.5) + annotate("segment", x=3,xend=3,y=0.2,yend=1.5,arrow=arrow(type="closed", length= unit(.4,"cm"))) + scale_fill_manual(values = list("lightgrey", "darkred"))+ scale_x_continuous(position = "top")+ xlab('Attributs (colonnes)') + ylab('Items (lignes)') + theme_modern() + theme(legend.position = 'none', axis.text=element_blank(), axis.line =element_blank()) ```