Commit d6068309 authored by Marc Oudart's avatar Marc Oudart

Fin de l'exo 4 module 2 et tests

parent 06721c27
......@@ -36,18 +36,21 @@ Tout d'abord on peut plotter les différents paramètres au cours du temps pour
```{r}
barplot(df$Appels_emis, names.arg = df$ï..Date)
```
Les dates sont mal positionnées mais c'est pas grave. Il semble que j'ai beaucoup appelé autour de la date du 17 mars.
2. Appels reçus
```{r}
barplot(df$Appels_recus, names.arg = df$ï..Date)
```
Il semble que j'ai reçu beaucoup d'appels la veille du 17 mars puis que j'ai reçu + d'appels en général après cette période qu'avant.
3. Durée appel
```{r}
barplot(df$Duree_appel, names.arg = df$ï..Date)
```
Là c'est très voyant. J'ai passé beaucoup de temps au téléphone après le 16 mars comparé à avant (sauf 2 fois).
4. Messages reçus
......@@ -59,6 +62,7 @@ barplot(df$Messages_recus, names.arg = df$ï..Date)
```{r}
barplot(df$Messages_envoyes, names.arg = df$ï..Date)
```
Pour les messages, la tendance est inverse aux appels : je reçois et envoie - de sms depuis le 17 mars comparé à avant.
J'ai eput être changé mon usage de l'un à l'autre.
Sauf autour du 16-17 mars où j'ai beaucoup communiqué.
......@@ -165,6 +169,222 @@ library(devtools)
#install_github("EvenStar69/significativity.bar/significativity.bar")
library(significativity.bar)
```
Bon j'ai dû un peu changer sa fonction parce qu'elle n'est plus compatible avec ggplot 3.3.
J'ai changé la manière de retrouver les coordonnées y : The position of ymax in ggplot\_build(plot)\$data[[1]] changed from column 6 to 7.
Et j'ai changé la manière de retrouver l'échalle en y : panel_ranges in ggplot\_build(plot)\$layout does not exist anymore ... use panel\_scale\_y instead.
J'ai aussi dû mettre à jour R vers la version 3.6.3.
Voici le code modifié de sa fonction :
```{r}
significativity_bar <- function(plot, groups, text = "*", text_height = 0.0275, size_bar = 1, color_bar = "black", size_text = 8, color_text = "black", font_face = 1, font_style = "serif", line_type = "solid"){
if (!require("ggplot2", character.only=T, quietly=T)){ # use library ggplot
install.packages("ggplot2")
library(ggplot2, character.only=T)
}
if (class(plot)[1] != "gg"){
stop("Your input plot is not a ggplot")
}
if (length(groups) != 2){
stop("Please select only 2 groups between which you want the error bar")
}
if (!is.vector(groups)){
stop("Please input your 2 selected groups in a vector")
}
if (!is.character(text)) {
stop("Please input the text above the bar as character")
}
if (!is.numeric(text_height) | length(text_height) > 1){
stop("Please input one numeric value for the text height")
}
if (!is.numeric(size_bar) | length(size_bar) > 1){
stop("Please input one numeric value for the bar size")
}
if (!is.character(color_bar)){
stop("Please input an existing R color, as a character, for the color of the bar")
}
if (!is.numeric(size_text) | length(size_text) > 1){
stop("Please input one numeric value for the text size")
}
if (!is.numeric(font_face) | length(font_face) > 1){
stop("Please input one numeric value for the font face")
}
if (!is.character(color_text)){
stop("Please input an existing R color, as a character, for the color of the text")
}
if (!is.character(font_style)){
stop("Please input an existing font family, as a character, for the color of the bar")
}
if (!is.character(line_type)){
stop("Please input an existing line style, as a character, for the color of the bar")
}
if (text_height >=1){
warning("text_height should be between 0 and 1, default value for * and around 0.04 for text are advised")
}
if (class(as.list.environment(plot$layers[[1]])$geom)[1] == "GeomPoint"){ # if the ggplot is a dotplot
coords = ggplot_build(plot)$data[[1]] # get the coordinates of the points
xcoords = c()
ycoords = c()
for (i in groups){ # get the x coordinates of all coordinates in a vector, for the 2 selected groups
xcoord_temp = unique(coords$x)[i]
xcoords = append(xcoords, xcoord_temp)
}
for (i in c(1,2)){
ycoord_temp = max(coords[coords$x == xcoords[i],]$y) # get the y coordinate of the upper point of each group
ycoords = append(ycoords, ycoord_temp)
}
y_range = ggplot_build(plot)$layout$panel_scales_y[[1]]$limits # get the total height of the y scale
# panel_ranges in ggplot_build(plot)$layout does not exist anymore ... use panel_scale_y instead
y_sum = sum(abs(y_range))
y_scale = (7.5/100)*y_sum # starting position of the vertical bar (determined % of the total y scale)
bar_height = y_scale + ((5/100)*y_sum) # final position of the vertical bar (determined % of the total y scale in addition to y_scale)
ycoord_top = max(ycoords) # the bar should take the heighest of the two groups as a reference
coord_bar = data.frame(x = c(xcoords[1], xcoords[1], xcoords[2], xcoords[2]), y = c(ycoord_top + y_scale, ycoord_top + bar_height, ycoord_top + bar_height, ycoord_top + y_scale)) # final coordinates of the bar
star_x = mean(xcoords) # x coordinate of the text above the bar (in the middle of the two groups)
star_y = ycoord_top + bar_height + ((2.75/100)*y_sum) # y coordinate of the text above the bar (above the bar by a determined factor)
coord_star = c(star_x, star_y) # x,y coordinates of the text above the bar
plot = plot + geom_path(data = coord_bar, aes(x=x, y=y), size = size_bar, color = color_bar, linetype = line_type) + annotate("text", x = star_x, y = star_y, label = text, size = size_text, color = color_text, fontface = font_face, family = font_style) # create the new ggplot
print(plot)
} else if (class(as.list.environment(plot$layers[[1]])$geom)[1] == "GeomBar") { # if the ggplot is a dotplot
coords = ggplot_build(plot)$data[[1]]
xcoords = c()
ycoords = c()
for (i in groups){ # get the x and y coordinates of the two groups
xcoord_temp = mean(c(coords[i,]$xmin, coords[i,]$xmax))
xcoords = append(xcoords, xcoord_temp)
ycoord_temp = coords[i,7] # The position of ymax in ggplot_build(plot)$data[[1]] changed from column 6 to 7
ycoords = append(ycoords, ycoord_temp)
}
y_range = ggplot_build(plot)$layout$panel_scales_y[[1]]$limits # get the total height of the y scale
y_sum = sum(abs(y_range))
y_scale = (7.5/100)*y_sum # starting position of the vertical bar (determined % of the total y scale)
bar_height = y_scale + ((5/100)*y_sum) # final position of the vertical bar (determined % of the total y scale in addition to y_scale)
ycoord_top = max(ycoords) # the bar should take the heighest of the two groups as a reference
coord_bar = data.frame(x = c(xcoords[1], xcoords[1], xcoords[2], xcoords[2]), y = c(ycoord_top + y_scale, ycoord_top + bar_height, ycoord_top + bar_height, ycoord_top + y_scale)) # final coordinates of the bar
star_x = mean(xcoords) # x coordinate of the text above the bar (in the middle of the two groups)
star_y = ycoord_top + bar_height + (text_height*y_sum) # y coordinate of the text above the bar (above the bar by a determined factor)
coord_star = c(star_x, star_y) # x,y coordinates of the text above the bar
plot = plot + geom_path(data = coord_bar, aes(x=x, y=y), size = size_bar, color = color_bar, linetype = line_type) + annotate("text", x = star_x, y = star_y, label = text, size = size_text, color = color_text, fontface = font_face, family = font_style) # create the new ggplot
print(plot)
}
}
```
```{r}
gg<- ggplot(c, aes(x = c..aa....ap.., y = as.numeric(m_duree_appel)))+
......@@ -172,6 +392,7 @@ gg<- ggplot(c, aes(x = c..aa....ap.., y = as.numeric(m_duree_appel)))+
geom_errorbar(ymin = m_duree_appel-sd_duree_appel, ymax = m_duree_appel+sd_duree_appel)+
ylim(-500,5000)+
labs(title="Duree appel")
significativity_bar(gg, groups = c(1,2))
```
......@@ -193,4 +414,7 @@ En moyenne c'est assez équilibré : je réponds autant de fois qu'on m'envoie u
## Conclusion
__J'ai passé pas mal de temps à faire ça mais ça m'a permis de bien prendre en main l'outil.
Je conçois que mon étude est assez sale et que les manières de plotter ne sont vraiment pas optimisées mais ce n'était pas vraiment le but de l'exercice.__
Je conçois que mon étude est assez sale et que les manières de plotter ne sont vraiment pas optimisées mais ce n'était pas vraiment le but de l'exercice.__
__Après avoir compilé, je n'ai pas pû le faire en pdf (des erreurs de polices qui ne passent pas avec LaTeX on dirait ...).
Aussi, j'ai remarqué qu'il fallait laissé un retour chariot après la visualisation du plot sinon le texte se met à côté dans le rendu.__
This source diff could not be displayed because it is too large. You can view the blob instead.
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500 64-bit) (preloaded format=pdflatex 2018.1.29) 7 APR 2020 21:22
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500 64-bit) (preloaded format=pdflatex 2018.1.29) 8 APR 2020 12:31
entering extended mode
**./Calcul_téléphone.tex
(Calcul_téléphone.tex
......
This diff is collapsed.
---
title: "R Notebook"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r}
plot(cars)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
This diff is collapsed.
---
title: "test"
author: "Marc"
date: "08/04/2020"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
J'ai changé la manière de retrouver les coordonnées y : The position of ymax in ggplot\_build(plot)\$data[[1]] changed from column 6 to 7.
Et j'ai changé la manière de retrouver l'échalle en y : panel_ranges in ggplot\_build(plot)\$layout does not exist anymore ... use panel\_scale\_y instead.
J'ai aussi dû mettre à jour R vers la version 3.6.3.
Voici le code modifié de sa fonction :
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment