diff --git a/module3/exo3/exercice_fr.Rmd b/module3/exo3/exercice_fr.Rmd index 7d5634946ac90bb4115f8796835be2b909d0beea..ba6725e3cd8f6e9f27540afb216db81ecf82c9e5 100644 --- a/module3/exo3/exercice_fr.Rmd +++ b/module3/exo3/exercice_fr.Rmd @@ -90,95 +90,137 @@ p ``` Nous pouvons maintenant ajouter la courbe et la surface correspondant aux salaires. + +- Graphe de base : prix du blé + courbe salaire +*Note* : l'option `expand = FALSE` permet de supprimer les espaces entre les axes et le graphe. L'option `clip = "off` permettra par la suité d'ajouter des annotations à l'extérieur du graphe. ```{r warning=FALSE} -# Graphe de base : prix du blé + courbe salaire p <- p + geom_line(aes(y = wages), color = "firebrick", na.rm = TRUE, size = 2.5) + - geom_area(aes(y = wages), fill = "lightblue", na.rm = TRUE, alpha = 0.9) + geom_area(aes(y = wages), fill = "lightblue", na.rm = TRUE, alpha = 0.9) + + coord_cartesian(expand = FALSE, clip = "off") p ``` On peut continuer à modifier ce graphe, pour se rapprocher au maximum de la représentation de William Fairplay. -***Note*** *: on ajoute volontairement un espace supplémentaire sur la partie supérieure du graphe (au-delà de la valeur y = 100) afin de produire par la suite les indications concernant les siècles. * + +- Modification des axes ```{r warning=FALSE} -# Modification des axes p <- p + - #geom_segment(aes(xend = year, y = 0, yend = wheat), linetype = "dotted", color = "black") + - scale_x_continuous(expand = expansion(add = c(0, 0)), + scale_x_continuous(limits = c(1565, 1830), breaks = c(1565, seq(1600, 1800, by = 50), 1830), minor_breaks = seq(1565, 1830, by = 5), - name = "5 Years each division") + - scale_y_continuous(expand = c(0, 0), limits = c(0, 109), breaks = seq(0, 100, by = 10), name = "", + name = "") + + scale_y_continuous(limits = c(0, 100), + breaks = seq(0, 100, by = 10), + name = "", sec.axis = sec_axis(~., name = "Price of the Quarter of Wheat in Shillings", breaks = seq(0, 100, by = 10))) p ``` +- Modification de l'arrière-plan et de la police ```{r warning=FALSE} -# Modification de l'arrière-plan et de la police p <- p + theme(panel.background = element_blank(), panel.grid.major = element_line(colour = "grey25", size = 0.8), panel.grid.minor = element_line(colour = "grey25", size = 0.3), text = element_text(family = "NewCenturySchoolbook", face = "italic"), - axis.title.x = element_text(hjust = 0.2, vjust = 1, size = 8), + axis.text = element_text(size = 7), axis.title.y.right = element_text(angle = 90, size = 8)) p ``` +- Ajout du titre et des annotations dans le graphe ```{r warning=FALSE} -# Ajout du titre dans le graphe et des annotations p <- p + annotate(geom = "label", x = 1650, y = 70, label = "CHART,\n Showing at One View\nThe Price of the Quater of Wheat,\n& Wages of Labour by the Week,\nfrom The Year 1565 to 1821,\nby William Playfair.", fontface = "bold.italic", size = 3.5, family = "NewCenturySchoolbook", label.r = unit(3, "lines")) + - annotate(geom = "text", x = c(1635, 1748), y = c(9, 18), label = "Weekly Wages of a Good Mechanic", - size = 2.7, angle = c(2, 10), family = "NewCenturySchoolbook") + annotate(geom = "text", x = c(1635, 1748), y = c(9, 18), label = "Weekly Wages of a Good Mechanic", + fontface = "italic", size = 2.7, angle = c(2, 10), family = "NewCenturySchoolbook") p ``` Pour la frise chronologique des rois et reines, nous allons utiliser les données disponibles [ici](https://mbostock.github.io/protovis/ex/wheat.js), et formattées au format CSV. -```{r message=FALSE, warning=FALSE} +```{r message=FALSE} # Fichier des rois et reines wheat_monarchs <- read_csv("wheat_monarchs.csv") wheat_monarchs %>% kable(format = "html", escape = FALSE, align ="c") %>% kable_styling(bootstrap_options = "striped", full_width = FALSE, position = "left") +``` -# Ajout frise chronologique des rois et reines +- Ajout frise chronologique des rois et reines +```{r warning=FALSE} p <- p + # rois/reines sur la barre supérieure geom_rect(data = wheat_monarchs %>% filter(row_number() %% 2 == 1, is.na(commonwealth)), aes(xmin = start, xmax = end, ymin = 97, ymax = 98), inherit.aes = FALSE) + - geom_text(data = wheat_monarchs %>% filter(row_number() %% 2 == 1, is.na(commonwealth)), aes(x = start + (end -start) / 2,y = 96, label = name), size = 3, family = "NewCenturySchoolbook", fontface = "italic") + + geom_text(data = wheat_monarchs %>% filter(row_number() %% 2 == 1, is.na(commonwealth)), aes(x = start + (end -start) / 2, y = 96, label = name), size = 2, family = "NewCenturySchoolbook", fontface = "italic") + # rois/reines sur la barre inférieure geom_rect(data = wheat_monarchs %>% filter(row_number() %% 2 == 0, is.na(commonwealth)), aes(xmin = start, xmax = end, ymin = 96, ymax = 97), inherit.aes = FALSE) + - geom_text(data = wheat_monarchs %>% filter(row_number() %% 2 == 0, is.na(commonwealth)), aes(x = start + (end -start) / 2,y = 95, label = name), size = 3, family = "NewCenturySchoolbook", fontface = "italic") + + geom_text(data = wheat_monarchs %>% filter(row_number() %% 2 == 0, is.na(commonwealth)), aes(x = start + (end -start) / 2, y = 95, label = name), size = 2, family = "NewCenturySchoolbook", fontface = "italic") + # cas particulier de Cromwell geom_rect(data = wheat_monarchs %>% filter(commonwealth), aes(xmin = start, xmax = end, ymin = 97, ymax = 98), inherit.aes = FALSE, fill = "white", color = "black") + - geom_text(data = wheat_monarchs %>% filter(commonwealth), aes(x = start + (end -start) / 2,y = 96, label = name), size = 3, family = "NewCenturySchoolbook", fontface = "italic") + geom_text(data = wheat_monarchs %>% filter(commonwealth), aes(x = start + (end -start) / 2, y = 96, label = name), size = 2, family = "NewCenturySchoolbook", fontface = "italic") p ``` +- Ajout des annotations en bas et à droite du graphe. +**Note** : en utilisant `annotation_custom`, on doit également modifier les marges autour du graphe. ```{r warning=FALSE} -# Ajout des siècles -p + - geom_rect(aes(xmin = 1565.5, xmax = 1829.5, ymin = 100, ymax = 109), fill = "white") + - annotate(geom = "text", - x = c(1580, 1650, 1750, 1820), y = 101.5, - label = c("16th Century", "17th Century", "18th Century", "19th Century"), - size = 2.7, family = "NewCenturySchoolbook") + - geom_curve(aes(x = 1565, y = 105, xend = 1600, yend = 100), curvature = -0.1, size = 1) + - geom_curve(aes(x = 1600, y = 100, xend = 1700, yend = 100), curvature = -0.17, size = 1) + - geom_curve(aes(x = 1700, y = 100, xend = 1800, yend = 100), curvature = -0.17, size = 1) + - geom_curve(aes(x = 1800, y = 100, xend = 1830, yend = 105), curvature = -0.1, size = 1) + - annotate(geom = "text", x = 1700, y = 104, label = "N°1", - size = 4, family = "NewCenturySchoolbook", fontface = "bold") -``` +p <- p + + theme(plot.margin = unit(c(1.2,0.8,0.5,0), units = "cm")) + + annotation_custom(grob = grid::textGrob(label = "shillings", + gp = grid::gpar(fontsize = 8, fontface = "italic", fontfamily = "NewCenturySchoolbook")), + xmin = 1844, xmax = 1844, ymin = 100, ymax = 100) + + annotation_custom(grob = grid::textGrob(label = "shillings", + gp = grid::gpar(fontsize = 8, fontface = "italic", fontfamily = "NewCenturySchoolbook")), + xmin = 1841, xmax = 1841, ymin = 0, ymax = 0) + + annotation_custom(grob = grid::textGrob(label = "5 Years each division", + gp = grid::gpar(fontsize = 8, fontface = "italic", fontfamily = "NewCenturySchoolbook")), + xmin = 1650, xmax = 1650, ymin = -5, ymax = -5) + + annotation_custom(grob = grid::textGrob(label = "5 Years each division", + gp = grid::gpar(fontsize = 8, fontface = "italic", fontfamily = "NewCenturySchoolbook")), + xmin = 1785, xmax = 1785, ymin = -5, ymax = -5) -Ces différentes étapes permettent d'obtenir un graphique assez proche de celui de William Playfair. +p +``` +- Enfin, ajout des annonations en haut du graphe. +```{r warning=FALSE} +p <- p + + annotation_custom(grob = grid::textGrob(label = "16th Century", + gp = grid::gpar(fontsize = 8, fontfamily = "NewCenturySchoolbook")), + xmin = 1577, xmax = 1577, ymin = 102.5, ymax = 102.5) + + annotation_custom(grob = grid::textGrob(label = "17th Century", + gp = grid::gpar(fontsize = 8, fontfamily = "NewCenturySchoolbook")), + xmin = 1650, xmax = 1650, ymin = 102.5, ymax = 102.5) + + annotation_custom(grob = grid::textGrob(label = "18th Century", + gp = grid::gpar(fontsize = 8, fontfamily = "NewCenturySchoolbook")), + xmin = 1750, xmax = 1750, ymin = 102.5, ymax = 102.5) + + annotation_custom(grob = grid::textGrob(label = "19th Century", + gp = grid::gpar(fontsize = 8, fontfamily = "NewCenturySchoolbook")), + xmin = 1820, xmax = 1820, ymin = 102.5, ymax = 102.5) + + annotation_custom(grob = grid::curveGrob(x1 = 0, y1 = 1, x2 = 1, y2 = 0, curvature = -0.12, square = FALSE, ncp = 20, + gp = grid::gpar(col = "black", lwd = 3)), + xmin = 1565, xmax = 1600, ymin = 105, ymax = 100) + + annotation_custom(grob = grid::curveGrob(x1 = 0, y1 = 0, x2 = 1, y2 = 1, curvature = -0.15, square = FALSE, ncp = 20, + gp = grid::gpar(col = "black", lwd = 3)), + xmin = 1600, xmax = 1700, ymin = 100, ymax = 100) + + annotation_custom(grob = grid::curveGrob(x1 = 0, y1 = 0, x2 = 1, y2 = 1, curvature = -0.15, square = FALSE, ncp = 20, + gp = grid::gpar(col = "black", lwd = 3)), + xmin = 1700, xmax = 1800, ymin = 100, ymax = 100) + + annotation_custom(grob = grid::curveGrob(x1 = 0, y1 = 0, x2 = 1, y2 = 1, curvature = -0.12, square = FALSE, ncp = 20, + gp = grid::gpar(col = "black", lwd = 3)), + xmin = 1800, xmax = 1830, ymin = 100, ymax = 105) + + annotation_custom(grob = grid::textGrob(label = "N°1", + gp = grid::gpar(fontsize = 11, fontfamily = "NewCenturySchoolbook", fontface = "bold")), + xmin = 1700, xmax = 1700, ymin = 104, ymax = 104) +p +``` +Ces différentes étapes permettent d'obtenir un graphique assez proche de celui de William Playfair.