Бесплатное программное обеспечение:
9L # целое число## [1] 9
-3.47 # рациональные числа## [1] -3.47
5+2i # мнимое число## [1] 5+2i
"Санкт-Петербург" # строка## [1] "Санкт-Петербург"
TRUE # логическое значение## [1] TRUE
FALSE # логическое значение## [1] FALSE
pi # число π## [1] 3.141593
2+2## [1] 4
9-4.5## [1] 4.5
117*2## [1] 234
234/2## [1] 117
5^7## [1] 78125
5**7## [1] 78125
25^0.5## [1] 5
25^(1/2) # скобки!## [1] 5
25^1/2 # скобки!## [1] 12.5
55%%3 # остаток от деления## [1] 1
sin(pi/2)## [1] 1
cos(pi)## [1] -1
sum(34, 23, 14)## [1] 71
prod(34, 23, 14)## [1] 10948
2:9## [1] 2 3 4 5 6 7 8 9
33:25## [1] 33 32 31 30 29 28 27 26 25
-5:-3## [1] -5 -4 -3
c(1,5,8,3)## [1] 1 5 8 3
rep(c(8, 2), 4)## [1] 8 2 8 2 8 2 8 2
rep(c(8, 2), each = 4)## [1] 8 8 8 8 2 2 2 2
c(TRUE, TRUE, FALSE, TRUE)[4]## [1] TRUE
c("alpha", "beta", "gamma")[2:3]## [1] "beta" "gamma"
c("alpha", "beta", "gamma")[-3]## [1] "alpha" "beta"
(1) Прибавьте к вектору
1:54
(2) Посчитайте \[\frac{\sum_{i=1}^{100}4 \times i^2}{1250}\]
a <- 5:24 # Сочетание Alt - ставит стрелочку
a[3]## [1] 7
a <- a + 3
a[3]## [1] 10
23 -> a
a## [1] 23
letters## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
a == 23 # равно?## [1] TRUE
a != 28 # не равно?## [1] TRUE
b <- 1:30
b[b %% 3 == 0] # индекс принимает условие!## [1] 3 6 9 12 15 18 21 24 27 30
(3) Сколько чисел от 1 до 1000 делится на 13?
height <- c(1, 1.7, 1.8, 1.74, 1.56, 1.66, 1.68, 1.77, 1.76, 6)
mean(height)## [1] 2.067
mean(height, trim = 0.1)## [1] 1.70875
weighted.mean(height, w = c(1,2,2,2,1,2,2,1,1,1))## [1] 1.95
range(height)## [1] 1 6
max(height)## [1] 6
min(height)## [1] 1
sd(height)## [1] 1.401381
median(height)## [1] 1.72
quantile(height)## 0% 25% 50% 75% 100%
## 1.0000 1.6650 1.7200 1.7675 6.0000
quantile(height, probs = c(0.1, 0.2))## 10% 20%
## 1.504 1.640
IQR(height) # разность 3ей и 2ой квартилей## [1] 0.1025
summary(height)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.665 1.720 2.067 1.768 6.000
(4) Является ли какая-то из приведенных мер хорошей?
df <- data.frame(name = c("a", "b", "c", "d", "f"),
height = c(1.76, 1.56, 1.45, 1.94, 1.81),
brown_hair = c(FALSE, TRUE, FALSE, TRUE, TRUE))
dfhead(df)tail(df)summary(df)## name height brown_hair
## a:1 Min. :1.450 Mode :logical
## b:1 1st Qu.:1.560 FALSE:2
## c:1 Median :1.760 TRUE :3
## d:1 Mean :1.704
## f:1 3rd Qu.:1.810
## Max. :1.940
df[1,]df[,2]## [1] 1.76 1.56 1.45 1.94 1.81
df$name## [1] a b c d f
## Levels: a b c d f
df <- read.table("путь к файлу")
df <- read.table("интернет ссылка")
df <- read.table(...) # разделитель: запятая
df <- read.table(..., sep = "\t") # разделитель: табуляция
df <- read.table(..., sep = ";") # разделитель: точка с запятойinstall.packages("ggplot2") # установить пакет
library("ggplot2") # включить пакетggplot2В работе Anscombe, F. J. (1973). “Graphs in Statistical Analysis” представлены следующие данные:

В раблоте Matejka and Fitzmaurice (2017) “Same Stats, Different Graphs” представлены следующие данные:

Дальше все примеры будут приводится на основании результатов работы Chi-kuk 2007. В эксперименте проверялась связь акустических мер и результатов перцептивного восприятия сексуальной ориентации. Носителей кантонского диалекта китайского языка попросили послушать записи носителей кантонского диалекта и оценить ориентацию тех, кого они слушали. В датасете 14 носителей и следующие переменные:
Скачиваем данные:
homo <- read.csv("http://goo.gl/Zjr9aF")
homoПредлагаю пробовать повторять все примеры с другим датасетом, представленным М. Даниэлем. Это данные из интервью с носителями одного из северных диалектов селения Устья. В датасете 62 носителя и следующие переменные:
ustya <- read.csv("https://goo.gl/FqsqSX", sep = "\t")
ustyaggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point()
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms,
color = orientation)) +
geom_point()
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms,
shape = orientation)) +
geom_point(color = "darkgreen")
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms,
size = age)) +
geom_point()
levels(homo$orientation) <- c("⚣", "⚤")
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms,
label = orientation, fill = orientation)) +
geom_label()
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms,
label = orientation, fill = orientation)) +
geom_text()
# Но для дальнейшей лекции имеет смысл вернуть обратно.
levels(homo$orientation) <- c("homo", "hetero")ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point()+
labs(title = "length of [s] vs. length of vowels",
subtitle = "based on 14 speakers of Cantonese",
caption = "data from [Chi kuk 2007]")
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point()+
xlab("duration of [s] in ms")+
ylab("vowel duration in ms")
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms, color = orientation)) +
geom_point() +
geom_rug()
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point() +
geom_hline(yintercept = mean(homo$vowel.duration.ms))+
geom_vline(xintercept = 60)
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point() +
geom_hline(yintercept = 120, linetype = 2)+
geom_vline(xintercept = 60, size = 5)
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point() +
geom_hline(yintercept = 120, linetype = 4)+
geom_vline(xintercept = 60, color = "blue")
The function annotate adds geoms to a plot.
ggplot(data = homo, aes(s.duration.ms, vowel.duration.ms)) +
geom_point()+
annotate(geom = "rect", xmin = 77, xmax = 79,
ymin = 117, ymax = 122, fill = "red", alpha = 0.2) +
annotate(geom = "text", x = 78, y = 125,
label = "Who is that?\n Outlier?")
Существует две возможности:
head(homo[, c(1, 9)])head(homo[, c(1, 10)])ggplot(data = homo, aes(orientation)) +
geom_bar()
ggplot(data = homo, aes(speaker, age)) +
geom_col()
ggplot(data = homo, aes(speaker, age, fill = orientation)) +
geom_bar(stat = "identity")
ggplot(data = homo, aes(orientation, s.duration.ms)) +
geom_boxplot()
ggplot(data = homo, aes(orientation, s.duration.ms)) +
geom_boxplot()+
geom_point()
ggplot(data = homo, aes(orientation, s.duration.ms)) +
geom_boxplot() +
geom_jitter(width = 0.5)
ggplot(data = homo, aes(orientation, s.duration.ms)) +
geom_violin() +
geom_jitter()
ggplot(data = homo, aes(s.duration.ms)) +
geom_histogram()
Сколько нужно ячеек?
ggplot(data = homo, aes(s.duration.ms)) +
geom_histogram(bins = nclass.FD(homo$s.duration.ms))
ggplot(data = homo, aes(s.duration.ms)) +
geom_histogram(fill = "lightblue")
ggplot(data = homo, aes(s.duration.ms)) +
geom_density()
ggplot(data = homo, aes(s.duration.ms)) +
geom_density(fill = "lightblue")
ggplot(data = homo, aes(s.duration.ms, fill = orientation)) +
geom_density()
ggplot(data = homo, aes(s.duration.ms, fill = orientation)) +
geom_density(alpha = 0.2)
library(ggjoy)
ggplot(data = homo, aes(s.duration.ms, orientation)) +
geom_joy()
ggplot(data = homo, aes(s.duration.ms, orientation, fill = orientation)) +
geom_joy()
facet_wrap()ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_wrap(~orientation)
ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_wrap(~orientation, scales = "free")
ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_wrap(~orientation, scales = "free_x")
facet_grid()homo$older_then_28 <- ifelse(homo$age > 28, "older", "younger")
ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_wrap(older_then_28~orientation, scales = "free_x")
ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_grid(older_then_28~orientation, scales = "free_x")
ggplot(data = homo, aes(speaker, s.duration.ms))+
geom_point() +
facet_grid(older_then_28~orientation, scales = "free_x", margins = TRUE)
ggplot(data = homo, aes(speaker, s.duration.ms))+
# Можно добавить geom без групирующей переменной!
geom_point(data = homo[,-9], aes(speaker, s.duration.ms), color = "grey") +
geom_point() +
facet_wrap(~orientation)+
theme_bw()