packages <- c("tidyverse", "palmerpenguins", "ggforce", "ggridges",
"ggbeeswarm", "ggdist")
install.packages(packages)
devtools::install_github('erocoar/gghalves')Boxplot, violinplot, histogram, stripplot, beeswarm, raincloud
Пакеты
library(tidyverse)Warning: package 'ggplot2' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 4.0.2 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Пингвины
penguins <- palmerpenguins::penguins
my_penguins <- drop_na(penguins)Квартили
quantile(c(1, 2, 4, 5, 8, 12, 14, 30)) 0% 25% 50% 75% 100%
1.0 3.5 6.5 12.5 30.0
quantile(c(1, 2, 4, 5, 8, 12, 14, 30), type = 2) 0% 25% 50% 75% 100%
1.0 3.0 6.5 13.0 30.0
Ящик с усами
boxplot(c(1, 2, 4, 5, 8, 12, 14, 30))boxplot(body_mass_g ~ species, data = my_penguins)ggplot(my_penguins) +
geom_boxplot(aes(x = species, y = body_mass_g), width = .5)Гистограммы
hist(my_penguins$body_mass_g)hist(my_penguins$body_mass_g,
breaks = 4)hist(my_penguins$body_mass_g,
breaks = 100)ggplot(my_penguins) +
geom_histogram(aes(x = body_mass_g, fill = species), bins = 40)Densityplot
density(c(1, 3, 3, 6)) %>% plot()density(c(1, 3, 3, 6), kernel = "triangular") %>% plot()density(c(1, 3, 3, 6), kernel = "rectangular") %>% plot()density(c(1, 3, 3, 6), kernel = "optcosine") %>% plot()density(c(1, 3, 3, 6), bw = .1) %>% plot()density(c(1, 3, 3, 6), bw = 2) %>% plot()ggplot(my_penguins) +
geom_density(aes(x = body_mass_g, fill = species)) ggplot(my_penguins) +
geom_density(aes(x = body_mass_g, fill = species), adjust = .2)ggplot(my_penguins) +
geom_density(aes(x = body_mass_g, fill = species), adjust = 2)ggplot(my_penguins) +
geom_density(aes(x = body_mass_g, fill = species), kernel = "triangular") Ridgelines (joyplot)
my_penguins %>%
ggplot(aes(x = body_mass_g, y = species)) +
ggridges::geom_density_ridges()Picking joint bandwidth of 153
my_penguins %>%
ggplot(aes(x = body_mass_g, y = species)) +
ggridges::geom_density_ridges() +
scale_y_discrete(expand = c(0, 0)) + # will generally have to set the `expand` option
scale_x_continuous(expand = c(0, 0)) +
coord_cartesian(clip = "off") +
ggridges::theme_ridges()Picking joint bandwidth of 153
Violinplot
ggplot(my_penguins) +
geom_density(aes(y = body_mass_g)) ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = ""))ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = species))ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = species)) +
coord_flip()ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = species),
quantiles = c(0.25, 0.5, 0.75),
quantile.linetype = c("solid"))ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = species),
quantiles = c(0.25, 0.5, 0.75),
quantile.linetype = c("dotted", "dashed", "dotted"))ggplot(my_penguins) +
geom_violin(aes(y = body_mass_g, x = species),
quantiles = seq(0.1, 0.9, by = 0.1),
quantile.linetype = c("dotted"))Stripplot
point + position=jitter
ggplot(data = my_penguins) +
geom_point(aes(x = species, y = body_mass_g))ggplot(data = my_penguins) +
geom_point(aes(x = species, y = body_mass_g), alpha = .7)ggplot(data = my_penguins) +
geom_point(aes(x = species, y = body_mass_g),
alpha = .7,
position = "jitter") +
geom_point(aes(x = species, y = body_mass_g), alpha = .7, colour = "purple")ggplot(data = my_penguins) +
geom_point(aes(x = species, y = body_mass_g),
alpha = .7,
position = position_jitter(width = .3, height = 0))ggplot(data = my_penguins) +
geom_jitter(aes(x = species, y = body_mass_g),
width = .3, height = 0)ggforce::sina() (точке в форме виоланчели)
ggplot(my_penguins) +
ggforce::geom_sina(aes(y = body_mass_g, x = species), position = "identity")ggplot(my_penguins) +
ggforce::geom_sina(aes(y = body_mass_g, x = species, colour = sex), position = "identity")Beeswarm
ggplot(my_penguins) +
ggbeeswarm::geom_quasirandom(aes(y = body_mass_g, x = species, fill = species), width = .35)ggplot(my_penguins) +
ggbeeswarm::geom_quasirandom(aes(y = body_mass_g, x = species, fill = species), width = .35, method = "smiley")ggplot(my_penguins) +
ggbeeswarm::geom_quasirandom(aes(y = body_mass_g, x = species, fill = species), width = .35, method = "frowney")ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species))ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), cex = 2)ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), cex = 2, method = "compactswarm")ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), cex = 2, method = "hex")Warning: In `position_beeswarm`, method `hex` discretizes the data axis (a.k.a the
continuous or non-grouped axis).
This may result in changes to the position of the points along that axis,
proportional to the value of `cex`.
To prevent this behavior, set `preserve.data.axis=TRUE`.
This warning is displayed once per session.
ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), cex = 2, method = "hex", side = 1)ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), cex = 2, method = "hex", side = -1)ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), method = "compactswarm", cex = 4)ggplot(data = my_penguins) +
ggbeeswarm::geom_beeswarm(aes(y = body_mass_g, x = species), method = "compactswarm", cex = 4, corral = "gutter")dotplot
ggplot(data = my_penguins) +
geom_dotplot(aes(x = body_mass_g), binwidth = 40,
dotsize = .9,
stackratio = 1.6)ggplot(data = my_penguins) +
geom_dotplot(aes(x = body_mass_g), binwidth = 40,
dotsize = .9,
stackratio = 1.6,
stackdir = "centerwhole")ggplot(data = my_penguins) +
geom_dotplot(aes(y = body_mass_g, x = species), binaxis = "y", stackdir = "center", dotsize = .6, stackratio = 1.2, binwidth = 100)Объединяем все вместе!
Склеивая разные слоя
ggplot(data = my_penguins) +
geom_violin(aes(x = species, y = body_mass_g, fill = species)) +
ggforce::geom_sina(aes(x = species, y = body_mass_g)) +
geom_boxplot(aes(x = species, y = body_mass_g), width = .1,
alpha = .8, outliers = FALSE)ggplot(data = my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin(aes(fill = species)) +
ggforce::geom_sina(size = .7) +
geom_boxplot(width = .1, alpha = .8, outliers = FALSE) +
scale_fill_brewer(palette = "Set1") +
hrbrthemes::theme_ipsum()ggplot(data = my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin(aes(fill = species)) +
ggforce::geom_sina(size = .7, shape = 21, colour = "grey35", fill = "#EECCBB") +
geom_boxplot(width = .1, alpha = .8, outliers = FALSE) +
scale_fill_brewer(palette = "Set1") +
hrbrthemes::theme_ipsum()position_nudge
ggplot(data = my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin(aes(fill = species), width = .2, position = position_nudge(x = .2)) +
geom_jitter(width = .1) +
geom_boxplot(width = .1, alpha = .8, position = position_nudge(x = -.2))violinplot + stat_summary()
ggplot(my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin() +
stat_summary(fun.data = mean_cl_normal)ggplot(my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin() +
stat_summary(shape = 10, size = .1, fun.data = mean_cl_normal,
colour = "brown")ggplot(my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin() +
stat_summary(geom = "errorbar", width = .05)No summary function supplied, defaulting to `mean_se()`
ggplot(my_penguins, aes(x = species, y = body_mass_g)) +
geom_violin() +
stat_summary(geom = "linerange", width = .05)Warning in stat_summary(geom = "linerange", width = 0.05): Ignoring unknown
parameters: `width`
No summary function supplied, defaulting to `mean_se()`
ggridges::geom_density_ridges(): Raincloud plot
ggplot(my_penguins) +
ggridges::geom_density_ridges(
aes(
x = body_mass_g,
y = species,
fill = species,
colour = species
),
jittered_points = TRUE,
alpha = 0.7
) Picking joint bandwidth of 153
ggplot(my_penguins) +
ggridges::geom_density_ridges(
aes(
x = body_mass_g,
y = species,
fill = species,
colour = species
),
jittered_points = TRUE,
position = "raincloud",
alpha = 0.7
) +
scale_fill_brewer(palette = "Set1") +
scale_colour_brewer(palette = "Set1") +
hrbrthemes::theme_ipsum()Picking joint bandwidth of 153
gghalves: половинки!
ggplot(my_penguins,
aes(y = body_mass_g, x = species, colour = species)) +
gghalves::geom_half_boxplot() ggplot(my_penguins,
aes(y = body_mass_g, x = species, colour = species)) +
gghalves::geom_half_boxplot() +
gghalves::geom_half_violin(position = position_dodge(width = 3), side = "r")ggplot(my_penguins,
aes(y = body_mass_g, x = species, colour = species)) +
gghalves::geom_half_boxplot() +
gghalves::geom_half_violin(position = position_dodge(width = 3), side = "r") +
gghalves::geom_half_dotplot(position = ggridges::position_points_sina(), dotsize = .2, binwidth = 100)