Modérateur : Groupe des modérateurs
Code : Tout sélectionner
habitatIF <- read.table(header = TRUE, text = "habitat IF_note_2015 IF_note_2020
6210 19 39
6210 19 24
4030 13 0")
library(tidyr)
habitatIF_long <- pivot_longer(
data = habitatIF,
cols = starts_with("IF_note"),
names_pattern = ".*_([0-9]+)",
names_to = "year",
values_to = "IF_note"
)
Code : Tout sélectionner
habitatIF_long
#> # A tibble: 6 x 3
#> habitat year IF_note
#> <int> <chr> <int>
#> 1 6210 2015 19
#> 2 6210 2020 39
#> 3 6210 2015 19
#> 4 6210 2020 24
#> 5 4030 2015 13
#> 6 4030 2020 0
Code : Tout sélectionner
ggplot(habitatIF) +
aes(x = year, y = IF_note, color = habitat)
geom_boxplot() +
facet_grid(~habitat) +
labs(x = "Année", y = "IF")
Code : Tout sélectionner
library(palmerpenguins)
library(ggplot2)
p_base <- ggplot(na.exclude(penguins)) +
aes(x = sex, y = body_mass_g) +
geom_boxplot() +
facet_grid(cols = vars(species, island))
p_base
Code : Tout sélectionner
library(data.table)
penguins_ttest <- as.data.table(na.exclude(penguins))[,
list(
pvalue = t.test(formula = body_mass_g ~ sex, data = .SD)$p.value,
x = 1.5,
y = max(body_mass_g) + diff(range(body_mass_g)) * 0.1
),
by = c("species", "island")
]
Code : Tout sélectionner
penguins_ttest[]
#> species island pvalue x y
#> 1: Adelie Torgersen 3.993168e-08 1.5 4880.0
#> 2: Adelie Biscoe 8.750160e-08 1.5 4967.5
#> 3: Adelie Dream 2.710669e-12 1.5 4825.0
#> 4: Gentoo Biscoe 1.867760e-28 1.5 6535.0
#> 5: Chinstrap Dream 2.264432e-06 1.5 5010.0
Code : Tout sélectionner
p_base +
geom_text(
data = penguins_ttest,
mapping = aes(
x = x,
y = y,
label = prettyNum(pvalue, scientific = TRUE, digits = 3, nsmall = 3)
)
)
Facundo Muñoz a écrit :Bonjour,
juste ajouter que si vous utilisez les facettes pour idéntifier les habitats, inutile de le faire en plus par les couleurs. Cela n'ajoute de l'information et par contre ça prenne de l'espace dans la figure et demande un effort cognitif pour l'interpréter.
Cordialement,
Code : Tout sélectionner
... +
geom_boxplot(..., show.legend = FALSE) +
...
Code : Tout sélectionner
... +
theme(legend.position = "none")
Code : Tout sélectionner
... +
guides(colour = "none")
Code : Tout sélectionner
library(palmerpenguins)
library(ggplot2)
library(ggpubr)
comparisons <- combn(
x = sort(unique(with(na.exclude(penguins), paste(species, island, sep = "\n")))),
m = 2,
simplify = FALSE
)
ggplot(na.exclude(penguins)) +
aes(
x = paste(species, island, sep = "\n"),
y = body_mass_g,
colour = paste(species, island)
) +
geom_boxplot(show.legend = FALSE) +
stat_compare_means(comparisons = comparisons, method = "t.test")
Code : Tout sélectionner
library(palmerpenguins)
library(ggplot2)
library(ggpubr)
dta <- na.exclude(penguins)
dta[["x"]] <- paste(dta[["species"]], dta[["island"]], sep = "\n")
Code : Tout sélectionner
comparisons <- combn(
x = sort(unique(with(na.exclude(penguins), paste(species, island, sep = "\n")))),
m = 2,
simplify = FALSE
)
ggplot(data = dta) +
aes(x = x, y = body_mass_g, colour = x) +
geom_boxplot(show.legend = FALSE) +
stat_compare_means(comparisons = comparisons, method = "t.test")
Code : Tout sélectionner
dta_annot <- compare_means(
formula = body_mass_g ~ x,
data = dta,
method = "t.test"
)
dta_annot <- dta_annot[dta_annot$p <= 0.05, ]
dta_annot[["y.position"]] <- max(dta[["body_mass_g"]]) +
diff(range(dta[["body_mass_g"]])) * seq(0.05, 0.20, length.out = nrow(dta_annot))
Code : Tout sélectionner
ggplot(data = dta) +
aes(x = x, y = body_mass_g, colour = x) +
geom_boxplot(show.legend = FALSE) +
stat_pvalue_manual(dta_annot, label = "p = {format(p, digits = 3, scientific = TRUE)}")
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité