Commit 915b9d9f authored by David Carayon's avatar David Carayon
Browse files

brand new themes, closes #1

parent f3174fa3
......@@ -30,4 +30,6 @@ Imports:
xfun,
magrittr,
cli,
rstudioapi
rstudioapi,
gridExtra,
bookdown
# Generated by roxygen2: do not edit by hand
export(bookdown_inrae)
export(inrae_report)
export(new_analysis)
export(palette_inrae)
export(scale_color_inrae)
export(scale_color_inrae_n5)
export(scale_color_quant)
export(scale_fill_inrae)
export(scale_fill_inrae_n5)
export(scale_fill_quant)
export(theme_inrae)
export(theme_inrae_1)
export(theme_quant)
import(ggplot2)
import(scales)
globalVariables("couleurs_inrae")
#' Template Bookdown respectant la charte graphique INRAE
#'
#' @param dir Le dossier dans lequel copier le projet bookdown
#'
#' @return
#' @export
bookdown_inrae <- function(dir = "MyProject"){
if (!dir.exists(dir)) {dir.create(dir)}
templates <- system.file("Bookdown_Inrae", package = "InraeThemes")
file.copy(templates, dir, recursive = TRUE)
setwd(file.path(dir,"Bookdown_Inrae"))
cli::cli_alert_warning(paste0("Working directory changed to '", file.path(dir,"Bookdown_Inrae"),"'"))
cli::cli_alert_info(paste0("Now opening '", "index.Rmd","'"))
rstudioapi::navigateToFile("index.Rmd")
}
#' Create analysis directory template
#' Architecture type d'une analyse
#'
#' @param dir the directory where the analysis template will be created
#' @param dir Le nom du dossier
#'
#' @return
#' @export
......@@ -50,92 +50,142 @@ output:
# Intro {-}
Refer to a [sub section](#sub-analysis).
Citing an article [@article].
Refer to section [Analysis].
Cf. [sous section](#Equations).
Citer un article [@article] (Penser à inclure le .bib dans le YAML)
Cf. section [Analyse].
# Input {-}
Loading libraries:
```{r Load libraries, message = FALSE}
On charge certains packages :
```{r packages, message = FALSE}
library(InraeThemes)
library(gt)
library(magrittr)
library(ggplot2)
library(xfun)
library(here)
library(ggplot2)
library(gt)
library(gridExtra)
```
# Analysis {-}
# Gestion des chemins d'accès
Afin de proposer une analyse facilement reproductible par quiconque, nous conseillons l'utilisation du package {here} pour la gestion des chemins d'accès.
Par exemple, pour pointer vers un jeu de données on va préférer :
```{r chemins, message = FALSE}
chemin_data <- here::here('data','donnees.csv') # Good
chemin_data <- 'data/donnees.csv' # Bad
```
# Gestion de version
Nous recommandons vivement l'utilisation d'un système de gestion de versions tel que **git**. Pour plus d'informations, vous pouvez consulter l'article dédié à git sur [le site de Rstudio](https://support.rstudio.com/hc/en-us/articles/200532077-Version-Control-with-Git-and-SVN)
# Tableaux {-}
`Vestibulum` imperdiet^[Nullam quis sem nunc], ex vel sodales facilisis, nibh tellus imperdiet massa, sit amet scelerisque orci velit vel tellus. Ut consequat justo tincidunt porttitor varius. Suspendisse erat ipsum, feugiat vitae rhoncus non, molestie ac purus. Morbi aliquet, elit eget blandit suscipit, est lacus facilisis turpis, nec fermentum nunc felis et lorem.
::: {#Table1}
A nice table example:
Exemple de tableau avec {gt}
:::
```{r}
gt::gt(head(mtcars)) %>% gt::tab_options(table.width = pct(100))
```
## Sub analysis {-}
## Equations {-}
> Suspendisse potenti
We can have math formulas inline like this: $E=mc^2$ or make them span an entire line like this:
Les formules LaTeX peuvent être utilisés au sein d'un paragraphe : $E=mc^2$ ou en tant qu'équation sur une ligne seule :
::: {#eq1}
:::
$$f=\\frac{a}{b+c}$$
**Create a horizontal line** using [markdown syntax](https://commonmark.org/help/):
----
Cras pulvinar ligula ac nisi porttitor, volutpat congue orci tincidunt. Pellentesque non mi congue, porta enim eget, venenatis sem. Integer suscipit vulputate tellus, eget commodo dolor gravida vel. Suspendisse gravida gravida ligula, in interdum sapien molestie ut.
### Yet another analysis {-}
### Graphiques {-}
Comme le notebook est un document HTML, vous pouvez inclure de la syntaxe CSS pour mettre en forme certains élements :
::: {.blue-box}
In ut vehicula risus.
Refer to the [table](#Table1) above!
Cf. [table](#Table1) ci-dessus
:::
```{r example plot, warning = FALSE, fig.align='center'}
options(scipen=999) # turn-off scientific notation like 1e+48
theme_set(theme_bw()) # pre-set the bw theme.
data(\"midwest\", package = \"ggplot2\")
# midwest <- read.csv(\"http://goo.gl/G1K41K\") # bkup data source
# Scatterplot
gg = ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method=\"loess\", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle=\"Area Vs Population\",
y=\"Population\",
x=\"Area\",
title=\"Scatterplot\",
caption = \"Source: midwest\")
plot(gg)
On reprend un des exemples proposés dans le README du package InraeThemes :
```{r example, message = FALSE, fig.width = 14, fig.height=8, dpi = 320}
## On charge les données d'exemple du package
data(\"example_datasets\")
## On construit 4 graphiques d'exemple, stockés dans une même liste
display <- list(
ggplot(example_datasets$www, aes_string(x = 'Minute', y = 'Users',
color = 'Measure',
shape = 'Measure')) +
geom_line() +
geom_point(size = 3) +
facet_wrap(~Measure) +
geom_point(size = 1.8) +
scale_color_inrae() +
scale_shape_manual(values = c(15, 16)) +
labs(title = \"Titre\", subtitle = \"Sous-titre\") +
theme_inrae(),
ggplot(example_datasets$cars, aes_string(x = 'mpg', fill = 'cyl',
colour = 'cyl')) +
geom_density(alpha = 0.75) +
scale_fill_inrae() +
scale_color_inrae() +
labs(fill = 'Cylinders', colour = 'Cylinders', x = 'MPG', y = 'Density') +
theme_inrae(),
ggplot(example_datasets$dia, aes_string(x = 'price', fill = 'cut')) +
geom_histogram(binwidth = 850) +
xlab('Price (USD)') +
ylab('Count') +
scale_fill_inrae() +
scale_x_continuous(label = function(x) paste0(x / 1000, 'k'))+
theme_inrae(),
ggplot(example_datasets$drivers, aes_string(x = 'Year', y = 'Deaths',
fill = \"Year\")) +
geom_boxplot(size = 0.25) +
ylab('Monthly Deaths') +
theme_inrae() +
scale_fill_inrae() +
coord_flip() +
labs(caption = \"Caption\")
)
# On assemble
do.call(gridExtra::grid.arrange, display)
```
Remember [equation 1](#eq1)!
# Conclusion {-}
Voici encore des exemples de mise en forme CSS :
::: {#box1 .green-box}
Wrapping it up!
En vert
:::
::: {#box4 .orange-box}
An orange box!!!
En orange
:::
# R session info {-}
Pensez à inclure cette section afin d'indiquer l'état de votre machine lorsque vous avez réalisé cette analyse
```{r session info, comment=\"\"}
xfun::session_info()
```
......@@ -155,7 +205,7 @@ cli::cli_alert_success(paste0("A new analysis directory was created with the fol
│ ├── raw_data
│ ├── README.md"))
cli::cli_alert_info(paste0("Now opening '", "README.md","'"))
rstudioapi::navigateToFile("README.md")
cli::cli_alert_info(paste0("Now opening '", "R/01_notebook.Rmd","'"))
rstudioapi::navigateToFile("R/01_notebook.Rmd")
}
#' Datasets fictifs pour l'illustration
#'
#' Cet objet contient une liste avec 4 datasets servant a l'illustration des themes.
#'
#' @format Quatre dataframes
"example_datasets"
#' Title
#' Palette de couleurs INRAE
#'
#' @param ...
#' Cette palette correspond aux couleurs issues de la charte INRAE V3.
#'
#' @return
#' @import scales
#' @return un vecteur contenant les couleurs
#' @export
scale_fill_inrae <- function(...){
discrete_scale("fill","inrae",manual_pal(values = c("#00a3a6","#275662","#66c1bf","#008c8e")), ...)
}
#' Title
#'
#' @param ...
#'
#' @return
#' @export
#' @import scales
scale_color_inrae <- function(...){
discrete_scale("color","inrae",manual_pal(values = c("#00a3a6","#275662","#66c1bf","#008c8e")), ...)
#' @examples
#' library(InraeThemes)
#' palette_inrae()
palette_inrae <- function() {
c("inrae" = "#00a3a6",
"inrae_clair" = "#66c1bf",
"inrae_fonce" = "#008c8e",
"inrae_sombre" = "#275662",
"inrae_vert" = "#9dc544",
"inrae_cyan" = "#9ed6e3",
"inrae_bleu" = "#423089",
"inrae_saumon" = "#ed6e6c",
"inrae_taupe_fonce" = "#797870",
"inrae_taupe_clair" = "#c4c0b3",
"inrae_gris" = "#4e5352")
}
#' Title
#'
#' @param ...
#'
#' @return
#' @import scales
#' @export
scale_fill_inrae_n5 <- function(...){
discrete_scale("fill","inrae",manual_pal(values = c("#275662","#008C56","#66C1BF","#9ED6E3","#C4C0B3")), ...)
}
#' Title
#' Echelle de couleur INRAE
#'
#' @param ...
#' @param ... Autres arguments pour \code{discrete_scale}.
#'
#' @return
#' @import scales
#' @export
scale_color_inrae_n5 <- function(...){
discrete_scale("color","inrae",manual_pal(values = c("#275662","#008C56","#66C1BF","#9ED6E3","#C4C0B3")), ...)
}
palette_light <- function() {
toupper(c(
blue = "#2c3e50", # blue
red = "#e31a1c", # red
green = "#18BC9C", # green
yellow = "#CCBE93", # yellow
steel_blue = "#a6cee3", # steel_blue
navy_blue = "#1f78b4", # navy_blue
light_green = "#b2df8a", # light_green
pink = "#fb9a99", # pink
light_orange = "#fdbf6f", # light_orange
orange = "#ff7f00", # orange
light_purple = "#cab2d6", # light_purple
purple = "#6a3d9a" # purple
))
}
#' Title
#'
#' @param ...
#'
#' @return
#' @import scales ggplot2
#' @export
scale_color_quant <- function(...) {
pal <- rep(unname(palette_light()), 100)
scale_color_manual(values = pal)
scale_fill_inrae <- function(...){
ggplot2::discrete_scale("fill","inrae",scales::manual_pal(values = c("#00a3a6","#275662","#9dc544","#9ed6e3","#423089","#ed6e6c","#797870","#c4c0b3","#4e5352")), ...)
}
#' Title
#' Echelle de remplissage INRAE
#'
#' @param ...
#' @param ... Autres arguments pour \code{discrete_scale}.
#'
#' @return
#' @import scales ggplot2
#' @export
scale_fill_quant <- function(...) {
pal <- rep(unname(palette_light()), 100)
scale_fill_manual(values = pal)
#' @import scales
scale_color_inrae <- function(...){
ggplot2::discrete_scale("color","inrae",scales::manual_pal(values = c("#00a3a6","#275662","#9dc544","#9ed6e3","#423089","#ed6e6c","#797870","#c4c0b3","#4e5352")), ...)
}
#' Title
#' Theme ggplot aux couleurs INRAE
#'
#' @param ...
#'
#' @return
#' @export
theme_inrae <- function(...){
theme(
axis.title = element_text(
family = "Avenir Next Pro",
size = 13,
color = "#275662",
face = "bold"
),
legend.title = element_text(
family = "Raleway",
size = 13,
color = "#275662",
face = "bold"
),
axis.text = element_text(
family = "Avenir Next Pro",
size = 13,
color = "#275662"
),
legend.text = element_text(
family = "Avenir Next Pro",
size = 13,
color = "#275662"
),
legend.position = "top",
strip.text = element_text(
family = "Avenir Next Pro",
size = 13,
color = "#275662"
),
plot.title = element_text(
family = "Raleway",
size = 20,
color = "#275662"
),
plot.subtitle = element_text(
family = "Raleway",
size = 16,
color = "#275662"
),
plot.caption = element_text(
family = "Avenir Next Pro",
size = 10,
color = "#275662",
hjust = -0.12
),
panel.background = element_rect(
color = "#275662",
fill = "white"
),
panel.grid.major.y = element_line(
color = "grey",
size = 0.3
),
panel.grid.minor.y = element_line(
color = "grey",
size = 0.3,
linetype = "dashed"
),
panel.grid.minor.x = element_line(
color = "grey",
size = 0.3,
linetype = "dashed"
)
)
}
#' Title
#' Ce theme ggplot correspond aux couleurs issues de la charte graphique INRAE V3.
#'
#' @param base_size base size
#' @param base_family base family
#'
#' @return
#' @export
theme_inrae_1 <- function(base_size = 10, base_family = "Avenir Next Pro") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
plot.title = element_text(
color = couleurs_inrae["vert_inrae_fonce"],
family = "Raleway",
face = "bold",
size = rel(1.2),
hjust = 0,
vjust = 1
),
plot.subtitle = element_text(
color = couleurs_inrae["vert_inrae"],
family = "Avenir Next Pro",
# face = 'bold',
size = rel(0.8),
hjust = 0,
vjust = 1
),
plot.caption = element_text(
color = couleurs_inrae["vert_inrae"],
family = "Avenir Next Pro",
hjust = 1,
# face = 'bold',
size = rel(0.8)
),
legend.text = element_text(colour = couleurs_inrae["vert_inrae"]),
legend.title = element_text(
colour = couleurs_inrae["vert_inrae_fonce"],
face = "bold",
hjust = 0
),
legend.background = element_rect(
fill = "transparent",
colour = NA
),
legend.key = element_rect(
fill = "transparent",
colour = NA
),
axis.ticks = element_line(color = couleurs_inrae["vert_inrae_fonce"]),
axis.text = element_text(colour = couleurs_inrae["vert_inrae"]),
axis.title.x = element_text(
colour = couleurs_inrae["vert_inrae_fonce"],
hjust = 1,
face = "bold"
),
axis.title.y = element_text(
colour = couleurs_inrae["vert_inrae_fonce"],
angle = 90,
hjust = 1,
face = "bold"
),
panel.background = element_rect(
fill = couleurs_inrae["blanc"],
colour = couleurs_inrae["vert_inrae_fonce"]
),
panel.grid.minor = element_line(
colour = couleurs_inrae["vert_inrae_clair"],
linetype = "dotted"
),
panel.grid.major = element_line(
colour = couleurs_inrae["vert_inrae_clair"],
linetype = "dashed"
),
plot.background = element_rect(
fill = NA,
colour = couleurs_inrae["vert_inrae_fonce"]
)
)
}
#' Title
#'
#' @param base_size base size
#' @param base_family base family
#' @import ggplot2
#'
#' @return
#' @export
theme_quant <- function(base_size = 15, base_family = "Avenir Next Pro") {
theme_inrae <- function(base_size = 15, base_family = "Avenir Next Pro") {
# Tidyquant colors
blue <- "#2c3e50"
green <- "#18BC9C"
# Global colors
blue <- palette_inrae()["inrae_bleu"]
green <- palette_inrae()["inrae_vert"]
white <- "#FFFFFF"
grey <- "grey80"
grey <- "#4e5352"
inrae <- palette_inrae()["inrae_fonce"]
inrae_sombre <- palette_inrae()["inrae_sombre"]
# Starts with theme_grey and then modify some parts
theme_grey(base_size = base_size, base_family = base_family) %+replace%
ggplot2::theme(
# Base Inherited Elements
line = ggplot2::element_line(colour = blue, size = 0.5, linetype = 1,
lineend = "butt"),
rect = ggplot2::element_rect(fill = white, colour = blue,
size = 0.5, linetype = 1),
text = ggplot2::element_text(family = base_family, face = "plain",
colour = blue, size = base_size,
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
margin = ggplot2::margin(), debug = FALSE),
line = ggplot2::element_line(colour = grey, size = 0.5, linetype = 1,lineend = "butt"),
rect = ggplot2::element_rect(fill = white, colour = blue,size = 0.5, linetype = 1),
text = ggplot2::element_text(family = base_family, face = "plain",
colour = inrae_sombre, size = base_size,
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
margin = ggplot2::margin(), debug = FALSE),
# Axes
axis.line = ggplot2::element_blank(),
axis.text = ggplot2::element_text(size = rel(0.8)),
axis.ticks = ggplot2::element_line(color = grey, size = rel(1/3)),
axis.title = ggplot2::element_text(size = rel(1.0), face = "bold"),
axis.line = ggplot2::element_blank(),
axis.text = ggplot2::element_text(size = rel(0.8)),
axis.ticks = ggplot2::element_line(color = grey, size = rel(1/3)),
axis.title = ggplot2::element_text(size = rel(1.0), face = "bold"),
# Panel
panel.background = ggplot2::element_rect(fill = white, color = NA),
panel.border = ggplot2::element_rect(fill = NA, size = rel(1/2), color = blue),
panel.grid.major = ggplot2::element_line(color = grey, size = rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = grey, size = rel(1/3)),
panel.grid.major = ggplot2::element_line(color = grey, size = rel(1/5), linetype = "longdash"),
panel.grid.minor = ggplot2::element_blank(),
panel.grid.minor.x = ggplot2::element_blank(),
panel.spacing = ggplot2::unit(.75, "cm"),
......@@ -204,20 +50,22 @@ theme_quant <- function(base_size = 15, base_family = "Avenir Next Pro") {
legend.title = element_text(face = "bold"),
# Strip (Used with multiple panels)
strip.background = ggplot2::element_rect(fill = blue, color = blue),
strip.background = ggplot2::element_rect(fill = inrae_sombre, color = blue),
strip.text = ggplot2::element_text(color = white, size = ggplot2::rel(0.8), margin = ggplot2::margin(t = 5, b = 5)),
# Plot
plot.title = ggplot2::element_text(size = rel(1.2), hjust = 0,
margin = ggplot2::margin(t = 0, r = 0, b = 4, l = 0, unit = "pt")),
plot.subtitle = ggplot2::element_text(size = rel(0.9), hjust = 0,
margin = ggplot2::margin(t = 0, r = 0, b = 3, l = 0, unit = "pt")),
plot.title = ggplot2::element_text(family = "Raleway",
color = inrae,
face = "bold",
size = rel(1.2), hjust = 0