library(ggplot2); library(dplyr); set.seed(42)
Присоединяю пакет: 'dplyr'
Следующие объекты скрыты от 'package:stats':
filter, lag
Следующие объекты скрыты от 'package:base':
intersect, setdiff, setequal, union
Визуализация в ggplot2 — файл для заполнения
library(ggplot2); library(dplyr); set.seed(42)
Присоединяю пакет: 'dplyr'
Следующие объекты скрыты от 'package:stats':
filter, lag
Следующие объекты скрыты от 'package:base':
intersect, setdiff, setequal, union
n <- 80
df <- data.frame(
age = sample(18:70, n, replace=TRUE),
weight = rnorm(n, 70, 12),
height = rnorm(n, 170, 10),
gender = sample(c("Male","Female"), n, replace=TRUE),
sport = sample(c("Yes","No"), n, replace=TRUE)
) |> mutate(BMI = weight/(height/100)^2)
head(df,8) age weight height gender sport BMI
1 66 53.58063 169.9818 Female No 18.54399
2 54 75.19382 165.7174 Male No 27.38078
3 18 60.26328 163.8633 Female No 22.44344
4 42 87.32922 149.7532 Male Yes 38.94101
5 27 64.82265 157.7525 Male No 26.04799
6 53 77.86777 171.7952 Female Yes 26.38371
7 35 73.86310 175.6762 Female No 23.93325
8 66 60.59393 165.0712 Male No 22.23752
# Гистограмма BMI + плотность (Ваш код здесь)
df |>
ggplot(aes(x = BMI, fill = gender)) +
geom_histogram(
aes(y = after_stat(density)),
position = "identity",
alpha = 0.4,
bins = 20
) +
geom_density(alpha = 0.6) +
labs(
x = "Индекс массы тела (BMI)",
y = "Плотность",
fill = "Пол"
) +
theme_minimal()# Scatter age vs BMI + lm-тренд (Ваш код здесь)
df |>
ggplot(aes(x = age, y = BMI, color = gender, shape = sport)) +
geom_point(alpha = 0.7, size = 2) +
geom_smooth(
aes(x = age, y = BMI),
method = "lm",
se = FALSE,
color = "black",
inherit.aes = FALSE
) +
labs(
x = "Возраст",
y = "Индекс массы тела (BMI)",
color = "Пол",
shape = "Спорт"
) +
theme_minimal()`geom_smooth()` using formula = 'y ~ x'
# Boxplot BMI по gender (Ваш код здесь)
df |>
ggplot(aes(x = gender, y = BMI, fill = gender)) +
geom_boxplot(alpha = 0.6) +
labs(
x = "Пол",
y = "Индекс массы тела (BMI)",
fill = "Пол"
) +
theme_minimal()# Violin height по gender + медиана (Ваш код здесь)
df |>
ggplot(aes(x = gender, y = height, fill = gender)) +
geom_violin(trim = FALSE, alpha = 0.6) +
stat_summary(
fun = median,
geom = "point",
size = 2,
color = "black"
) +
labs(
x = "Пол",
y = "Рост (см)",
fill = "Пол"
) +
theme_minimal()# Групповые средние и barplot (Ваш код здесь)
bmi_means <- df |>
group_by(gender, sport) |>
summarise(mean_BMI = mean(BMI), .groups = "drop")
bmi_means# A tibble: 4 × 3
gender sport mean_BMI
<chr> <chr> <dbl>
1 Female No 24.4
2 Female Yes 24.7
3 Male No 24.4
4 Male Yes 25.0
bmi_means |>
ggplot(aes(x = gender, y = mean_BMI, fill = sport)) +
geom_col(position = "dodge") +
labs(
x = "Пол",
y = "Средний BMI",
fill = "Спорт"
) +
theme_minimal()# Facets и пользовательские цвета/темы (Ваш код здесь)Опишите основные наблюдения (1–2 абзаца).
Так как данные были синтетически сгенерированы, то рассматривать кореляции не имеет смысла, все распределения либо равномерные либо нормальные и указаны во втором чанке с кодом.