library(knitr)
library(readr)
library(psych)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ dplyr 1.0.7
## ✓ tibble 3.1.6 ✓ stringr 1.4.0
## ✓ tidyr 1.1.4 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x ggplot2::%+%() masks psych::%+%()
## x ggplot2::alpha() masks psych::alpha()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(dplyr)
library(ggridges)
data(iris)
attach(iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
dim(iris)
## [1] 150 5
names(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
describe.by(Sepal.Length, Species)
## Warning: describe.by is deprecated. Please use the describeBy function
##
## Descriptive statistics by group
## group: setosa
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 50 5.01 0.35 5 5 0.3 4.3 5.8 1.5 0.11 -0.45 0.05
## ------------------------------------------------------------
## group: versicolor
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 50 5.94 0.52 5.9 5.94 0.52 4.9 7 2.1 0.1 -0.69 0.07
## ------------------------------------------------------------
## group: virginica
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 50 6.59 0.64 6.5 6.57 0.59 4.9 7.9 3 0.11 -0.2 0.09
hist(iris$Sepal.Length,
col='steelblue',
main='Histogram',
xlab='Length',
ylab='Frequency')
plot(iris$Sepal.Width, iris$Sepal.Length,
col=c("steelblue", "Red", "Green"),
main='Scatterplot',
xlab='Sepal Width',
ylab='Sepal Length',
pch=19)
library(viridis) # Cần cài packages viridis trước
## Loading required package: viridisLite
cols = viridis(3) # 3 là số đối tượng cần gán màu
# Gán màu vào vd trên
plot(iris$Sepal.Width, iris$Sepal.Length,
col=cols,
main='Scatterplot',
xlab='Sepal Width',
ylab='Sepal Length',
pch=19)
boxplot(Sepal.Length~Species,
data=iris,
main='Sepal Length by Species',
xlab='Species',
ylab='Sepal Length',
col=c("Blue", "Red", "Green"),
border='black')
ggplot(data = iris)
ggplot(data = iris ) +
aes(x = Petal.Length, y = Petal.Width)
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point()
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point( aes(color = Species, shape = Species) )
ggplot(data = iris) +
aes(x = Petal.Length, y = Petal.Width) +
geom_point( aes(color = Species, shape = Species) ) +
geom_smooth(method =lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(data = iris) +
aes(x = Species, y = Sepal.Length, color = Species) +
geom_boxplot()
ggplot(data = iris) +
aes(x = Species, y = Sepal.Length, color = Species) +
geom_boxplot() +
geom_jitter(position = position_jitter(0.2))
ggplot(data = iris) +
aes(x = Petal.Length, fill = Species) +
geom_density( alpha = 0.3)
ggplot(data = iris) +
aes(x = Petal.Length, fill = Species) +
geom_density( alpha = 0.3) +
facet_wrap(~ Species, nrow = 3)
ggplot(iris, aes(x = Petal.Length, y = Species, fill = Species)) +
geom_density_ridges() +
theme_ridges() + # No color on backgroud
theme(legend.position = "none", # No show legend
axis.title.x = element_text(hjust = 0.5), # x axis title in the center
axis.title.y = element_text(hjust = 0.5)) # y axis title in the center
## Picking joint bandwidth of 0.155
Hết: Ngọc Nguyễn