R數(shù)據(jù)科學(xué)第五章
library(tidyverse)
變量的分布進(jìn)行可視化
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
圖片.png
- 觀測(cè)值可以使用dplyr::count()計(jì)算統(tǒng)計(jì)
diamonds %>%
count(cut)
圖片.png
連續(xù)變量,用直方圖
ggplot(data = diamonds) +
geom_histogram(mapping = aes(x = carat), binwidth = 0.5)
圖片.png
-
當(dāng)然也可以用dplry::count和ggplot::cut_width進(jìn)行手動(dòng)統(tǒng)計(jì)
diamonds %>% count(cut_width(carat, 0.5))
圖片.png 根據(jù)自己的目的選擇直方圖的寬度矮锈,如果選擇小于3克拉的鉆石
smaller <- diamonds %>%
filter(carat < 3)
ggplot(data = smaller, mapping = aes(x = carat)) +
geom_histogram(binwidth = 0.1)
圖片.png
- 如果使用疊加的條形圖則用geom_freqpoly代替geom_histogram,但前者是折線圖統(tǒng)計(jì)
ggplot(data = smaller, mapping = aes(x = carat, color = cut)) +
geom_histogram(binwidth = 0.1)
ggplot(data = smaller, mapping = aes(x = carat, color = cut)) +
geom_freqpoly(binwidth = 0.1)
ggplot(data = smaller, mapping = aes(x = carat)) +
geom_histogram(binwidth = 0.1)
ggplot(data = faithful, mapping = aes(x = eruptions)) +
geom_histogram(binwidth = 0.25)
圖片.png
圖片.png
5.3異常值
ggplot(diamonds) +
geom_histogram(mapping = aes(x = y), binwidth = 0.5)
圖片.png
- coord_cartesian()放到靠近0的數(shù)值
ggplot(diamonds) +
geom_histogram(mapping = aes(x = y), binwidth =0.5) +
coord_cartesian(ylim = c(0,50))
圖片.png
- 可以看到有三個(gè)異常的數(shù)值,利用dplry中的filter將他們 找到
unusual <- diamonds %>%
filter(y < 3 | y > 20) %>%
arrange(y)
unusual
圖片.png
ggplot(unusual) +
geom_histogram(mapping = aes(x = y), binwidth =0.5) +
coord_cartesian(ylim = c(0,50))
圖片.png
鉆石為0.99克拉和1克拉的數(shù)量如失,為什么出現(xiàn)這樣的結(jié)果
m0.99 <- diamonds %>%
filter(carat == 0.99)
m0.99
m1 <- diamonds %>%
filter(carat == 1)
m1
圖片.png
- 缺失值
diamonds2 <- diamonds %>%
filter(between(y, 3, 20)) %>%
arrange(y)
diamonds2
- 利用缺失值NA代替異常值,mutate()
diamonds3 <- diamonds %>%
mutate(y = ifelse(y < 3|y >20, NA, y))
diamonds3
- ggplot2中遵循無視缺失值的原則鸠踪,忽略缺失值
ggplot(data = diamonds3, mapping = aes(x = x, y = y)) +
geom_point()
###Warning message:
Removed 9 rows containing missing values (geom_point).
-
警告忽略缺失值,可用na.rn = TURE,消除警告
ggplot(data = diamonds3, mapping = aes(x = x, y = y)) + geom_point(na.rm = TRUE)
圖片.png
- 下邊這個(gè)沒看懂 原文是:弄清楚造成缺失值的觀測(cè)和沒有缺失值的觀測(cè)間的區(qū)別的原因,例如:在nycflights13::flights 中跟压,dep_time變量中的缺失值表示 航班取消了,因子架忌,應(yīng)該比較一下 已取消的航班和未取消航班的計(jì)劃出發(fā)時(shí)間愧沟,利用is.na()函數(shù)創(chuàng)建一個(gè)新變量來完成這個(gè)操作
nycflights13::flights %>%
mutate(
cancelled = is.na(dep_time),
sched_hour = sched_dep_time %/% 100,
sched_min = sched_dep_time %% 100,
sched_dep_time = sched_hour + sched_min / 60
) %>%
ggplot(mapping = aes(sched_dep_time)) +
geom_freqpoly(
mapping = aes(color = cancelled),
binwidth = 1/4)
圖片.png
相關(guān)變動(dòng)是兩個(gè)或者多個(gè)變量以相關(guān)的方式共同變化所表現(xiàn)出的趨勢(shì)
- ** 分類變量和連續(xù)變量**
ggplot(data = diamonds, mapping = aes(x = price)) +
geom_freqpoly(mapping = aes(color = cut), binwidth = 500)
圖片.png
- 注:應(yīng)為數(shù)量差別很大和難看出差距
- 將縱坐標(biāo)改成密度 density,相當(dāng)于對(duì)計(jì)數(shù)進(jìn)行了標(biāo)準(zhǔn)化粤咪,這樣每個(gè)頻率多邊形下面的面積都是1
ggplot(data = diamonds, mapping = aes(x = price, y = ..density..)) +
geom_freqpoly(mapping = aes(color = cut), binwidth = 500)
圖片.png
箱線圖可以將分類變量進(jìn)行可視化
- geom_boxplot函數(shù)查看切割質(zhì)量和價(jià)格分布
ggplot(data = diamonds, mapping = aes(x = cut, y = price)) +
geom_boxplot(mapping = aes(color = cut)
)
圖片.png
- 利用reorder()函數(shù)進(jìn)行排列
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
ggplot(data = mpg, mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) +
geom_boxplot()
圖片.png
圖片.png
- 利用coord_flip將函數(shù)圖形旋轉(zhuǎn)90度
ggplot(data = mpg, mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) +
geom_boxplot() +
coord_flip()
圖片.png
個(gè)人學(xué)習(xí)筆記谚中,記錄的不夠詳細(xì),比較粗糙寥枝,勿噴宪塔。