#источник: https://t.me/rstudioprogr
library(ggplot2)
library(gridExtra)
library(dplyr)
##
## Присоединяю пакет: 'dplyr'
## Следующий объект скрыт от 'package:gridExtra':
##
## combine
## Следующие объекты скрыты от 'package:stats':
##
## filter, lag
## Следующие объекты скрыты от 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
x <- rnorm(1000,0,1)
y <- rnorm(1000,1000,50)
data <- data.frame(x = x, y = y)
data1 <- data %>% pivot_longer(cols = "x":"y", names_to = "name", values_to = "values")
g1 <- ggplot(data = data1, aes(x = name, y = values, fill = name)) + geom_boxplot() + theme_bw() + scale_fill_manual(values = c("darkblue", "lightblue")) + ggtitle("Пример №1. Некорректный")
g1

gapminder <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
str(gapminder)
## 'data.frame': 1704 obs. of 6 variables:
## $ country : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ pop : num 8425333 9240934 10267083 11537966 13079460 ...
## $ continent: chr "Asia" "Asia" "Asia" "Asia" ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ gdpPercap: num 779 821 853 836 740 ...
g2 <- ggplot(data = gapminder, aes(x = continent, y = lifeExp, fill = continent)) + geom_boxplot() + theme_bw() + scale_fill_manual(values = c("#be1f33", "#ffffff", "#9ed5dc", "#2f8791", "#3d6f56")) + ggtitle("Пример №2. Распределение ожидаемой
продолжительности жизни")
g2

g3 <- ggplot(data = gapminder, aes(x = continent, y = lifeExp, fill = continent)) + geom_boxplot() + theme_bw() + geom_jitter(alpha = 0.5, aes(col = continent)) +scale_fill_manual(values = c("#be1f33", "#ffffff", "#9ed5dc", "#2f8791", "#3d6f56")) + ggtitle("Пример №3. Распределение ожидаемой
продолжительности жизни") + scale_color_manual(values = c("#be1f33", "#ffffff", "#9ed5dc", "#2f8791", "#3d6f56"))
g3
