Réduction du temps de travail sous R
INRAE UMR EcoSys
7/4/23
R
── Data Summary ────────────────────────
Values
Name penguins
Number of rows 344
Number of columns 8
_______________________
Column type frequency:
factor 3
numeric 5
________________________
Group variables None
── Variable type: factor ───────────────────────────────────────────────────────
skim_variable n_missing complete_rate ordered n_unique
1 species 0 1 FALSE 3
2 island 0 1 FALSE 3
3 sex 11 0.968 FALSE 2
top_counts
1 Ade: 152, Gen: 124, Chi: 68
2 Bis: 168, Dre: 124, Tor: 52
3 mal: 168, fem: 165
── Variable type: numeric ──────────────────────────────────────────────────────
skim_variable n_missing complete_rate mean sd p0 p25 p50
1 bill_length_mm 2 0.994 43.9 5.46 32.1 39.2 44.4
2 bill_depth_mm 2 0.994 17.2 1.97 13.1 15.6 17.3
3 flipper_length_mm 2 0.994 201. 14.1 172 190 197
4 body_mass_g 2 0.994 4202. 802. 2700 3550 4050
5 year 0 1 2008. 0.818 2007 2007 2008
p75 p100 hist
1 48.5 59.6 ▃▇▇▆▁
2 18.7 21.5 ▅▅▇▇▂
3 213 231 ▂▇▃▅▂
4 4750 6300 ▃▇▆▃▂
5 2009 2009 ▇▁▇▁▇
library(tidyverse); library(palmerpenguins); library(report) # Import package & données
fit <- lm(body_mass_g ~ flipper_length_mm * species, # Régression ailes x masse
data = penguins)
ggplot(penguins, # Représentation graphique
aes(y = body_mass_g, x = flipper_length_mm, color = species)) +
geom_point() +
geom_smooth(method = "lm", se = F) +
labs(x = "Poids (g)", y = "Taille des ailes (mm)", colour = "Espèce") +
theme_bw(18)
\[\small{ Masse \sim \begin{aligned} \beta_0 + \beta_1 Aile + \beta_2 Esp._{[Chinstrap]} + \beta_3 Esp._{[Gentoo]} + \\ \beta_4 Aile \times Esp._{[Chinstrap]} + \beta_5 Aile \times Esp._{[Gentoo]} \end{aligned} } \]
Parameter | Coefficient | CI | CI_low | CI_high | t | df_error | p |
---|---|---|---|---|---|---|---|
(Intercept) | -2535.84 | 0.95 | -4265.79 | -805.88 | -2.88 | 336 | 0.00 |
flipper length mm | 32.83 | 0.95 | 23.73 | 41.93 | 7.10 | 336 | 0.00 |
species [Chinstrap] | -501.36 | 0.95 | -3498.08 | 2495.36 | -0.33 | 336 | 0.74 |
species [Gentoo] | -4251.44 | 0.95 | -7059.08 | -1443.81 | -2.98 | 336 | 0.00 |
flipper length mm × species [Chinstrap] | 1.74 | 0.95 | -13.71 | 17.19 | 0.22 | 336 | 0.82 |
flipper length mm × species [Gentoo] | 21.79 | 0.95 | 8.14 | 35.44 | 3.14 | 336 | 0.00 |
ggplot2
gganimate
Dynamiques prédateurs-proies Lotka-Volterra
\[\small{ \begin{align} \frac{dH}{dt}=H_t(b_H-L_tm_H)\\ \frac{dL}{dt}=L_t(H_tb_L-m_L)\\ \end{align}}\]
library(deSolve); library(tidyverse); library(gganimate)
# Full example: https://strimas.com/post/lotka-volterra/
# parameters
pars <- c(bH = 1, mH = 0.2, bL = 0.1, mL = 0.2)
# initial state
init <- c(H = 1, L = 2)
# times
t <- seq(0, 100, by = 1)
deriv <- function(t, state, pars) {
with(as.list(c(state, pars)), {
dHdt <- H*(bH - L*mH)
dLdt <- L*(H*bL - mL)
return(list(c(H = dHdt, L = dLdt)))
})
}
lv_results <- ode(init, t, deriv, pars)
p <- lv_results %>%
data.frame() %>%
pivot_longer(cols = c(H,L), values_to = "Nb", names_to = "pop") %>%
mutate(pop = if_else(pop == "H", "Prey", "Predator")) %>%
ggplot(aes(x = time, y = Nb, color = pop, group = pop)) +
geom_line(linewidth = 1) +
geom_point(size = 3, alpha = .8) +
scale_color_brewer(NULL, palette = "Set1") +
labs(title = "Lotka-Volterra predator prey model",
subtitle = paste(names(pars), pars, sep = " = ", collapse = "; "),
x = "Time", y = "Population density") +
theme_bw(16)
anim <- p + transition_reveal(time)
anim_save("images/anim.gif", anim, width = 800, height = 400)
gganimate
ggiraph
library(tibble) ; library(ggiraph) ; library(magrittr)
gg_point <- ggplot(
data = mtcars,
mapping = aes(x = wt, y = mpg, size = disp, color = as.factor(carb))
) +
geom_point_interactive(aes(tooltip = row.names(mtcars), data_id = row.names(mtcars))) +
scale_color_brewer(palette = "Set1", name = "carb") +
scale_size(range = c(1, 15), name = "disp") +
scale_x_continuous(limits = c(1, 6)) +
scale_y_continuous(limits = c(7, 36)) +
theme_minimal() +
theme(legend.position = "bottom")
girafe_obj <- girafe(ggobj = gg_point, width_svg = 5.9,
height_svg = 3) %>%
girafe_options(
opts_tooltip(opacity = .7),
opts_zoom(min = .5, max = 4),
opts_hover(css = "fill:red;stroke:white;stroke-width:2px;")
)
girafe_obj
leaflet
library(leaflet); library(raster) ; library(htmlwidgets)
usa <- getData("GADM", country="FRANCE", level=2)
usa$randomData <- rnorm(n=nrow(usa), 150, 30)
#create a color palette to fill the polygons
pal <- colorQuantile("Greens", NULL, n = 5)
#create a pop up (onClick)
polygon_popup <- paste0("<strong>Name: </strong>", usa$NAME_1, "<br>",
"<strong>Indicator: </strong>", round(usa$randomData,2))
#create leaflet map
map = leaflet(width = "100%") %>%
addProviderTiles("CartoDB.Positron", group = "Positron") %>%
addTiles(urlTemplate = "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}", "© Google", group = "Satellite") %>%
addTiles(urlTemplate = "https://wxs.ign.fr/choisirgeoportail/geoportail/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&STYLE=PCI vecteur&TILEMATRIXSET=PM&FORMAT=image/png&LAYER=CADASTRALPARCELS.PARCELLAIRE_EXPRESS&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}", "© IGN", group = "Cadastre") %>%
addPolygons(data = usa,
fillColor= ~pal(randomData),
fillOpacity = 0.4,
weight = 2,
color = "white",
popup = polygon_popup) %>%
addLayersControl(baseGroups = c("Positron", "Satellite", "Cadastre"), options = layersControlOptions(collapsed = FALSE)) %>%
addScaleBar(position = "bottomleft",
options = scaleBarOptions(metric = TRUE, imperial = FALSE)) %>%
addMeasure(
position = "topright",
primaryLengthUnit = "meters",
primaryAreaUnit = "hectares",
localization = "fr",
activeColor = "red",
completedColor = "#7D4479"
)
map