[R語言]數據可視化的最佳解決方案:ggplot2教程及實例

前言

ggplot是一個擁有一套完備語法且容易上手的繪圖系統,在PythonR中都能引入並使用,在數據分析可視化領域擁有極爲普遍的應用。本篇從R的角度介紹如何使用ggplot2包,首先給幾個我以爲最值得推薦的理由:html

  • 採用「圖層」疊加的設計方式,一方面能夠增長不一樣的圖之間的聯繫,另外一方面也有利於學習和理解該packagephotoshop的老玩家應該比較能理解這個帶來的巨大便利
  • 適用範圍廣,擁有詳盡的文檔,經過?和對應的函數便可在R中找到函數說明文檔和對應的實例
  • RPython中都可使用,下降兩門語言之間互相過渡的學習成本

基本概念

本文采用ggplot2的自帶數據集diamondspython

> head(diamonds)
# A tibble: 6 x 10
  carat cut       color clarity depth table price     x     y     z
  <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48

# 變量含義
price  : price in US dollars (\$326–\$18,823)
carat  : weight of the diamond (0.2–5.01)
cut    :   quality of the cut (Fair, Good, Very Good, Premium, Ideal)
color  : diamond colour, from D (best) to J (worst)
clarity: a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1, IF (best))
x      : length in mm (0–10.74)
y      : width in mm (0–58.9)
z      : depth in mm (0–31.8)
depth  : total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43–79)
table  : width of top of diamond relative to widest point (43–95)

基於圖層和畫布的概念,ggplot2引伸出以下的語法框架:面試

圖源: https://mp.weixin.qq.com/s/us...

  • data:數據源,通常是data.frame結構,不然會被轉化爲該結構
  • 個性映射與共性映射:ggplot()中的mapping = aes()參數屬於共性映射,會被以後的geom_xxx()stat_xxx()所繼承,而geom_xxx()stat_xxx()中的映射參數屬於個性映射,僅做用於內部
  • mapping:映射,包括顏色類型映射color;fill、形狀類型映射linetype;size;shape和位置類型映射x,y
  • geom_xxx:幾何對象,常見的包括點圖、折線圖、柱形圖和直方圖等,也包括輔助繪製的曲線、斜線、水平線、豎線和文本等
  • aesthetic attributes:圖形參數,包括colour;size;hape
  • facetting:分面,將數據集劃分爲多個子集subset,而後對於每一個子集都繪製相同的圖表
  • theme:指定圖表的主題
ggplot(data = NALL, mapping = aes(x = , y = )) +   # 數據集
    geom_xxx()|stat_xxx() +       # 幾何圖層/統計變換
    coord_xxx() +  # 座標變換, 默認笛卡爾座標系     
    scale_xxx() +  # 標度調整, 調整具體的標度  
    facet_xxx() +  # 分面, 將其中一個變量進行分面變換  
    guides() +     # 圖例調整
    theme()        # 主題系統
這些概念能夠等看徹底文再回過頭看,至關於一個彙總,這些概念都掌握了基本 ggplot2的核心邏輯也就理解了

一些核心概念的含義能夠從RStudio官方的cheat sheet圖中大體得知:
算法

一些栗子

經過實例和 RCode從淺到深介紹 ggplot2的語法。

1. 五臟俱全的散點圖

library(ggplot2)

# 代表咱們使用diamonds數據集, 
ggplot(diamonds) + 
  # 繪製散點圖: 橫座標x爲depth, 縱座標y爲price, 點的顏色經過color列區分,alpha透明度,size點大小,shape形狀(實心正方形),stroke點邊框的寬度
  geom_point(aes(x = carat, y = price, colour = color), alpha=0.7, size=1.0, shape=15, stroke=1) +
  # 添加擬合線
  geom_smooth(aes(x = carat, y = price), method = 'glm') +
  # 添加水平線
  geom_hline(yintercept = 0, size = 1, linetype = "dotted", color = "black") +
  # 添加垂直線
  geom_vline(xintercept = 3, size = 1, linetype = "dotted", color = "black") +
  # 添加座標軸與圖像標題
  labs(title = "Diamonds Point Plot", x = "Carat", y = "Price") +
  # 調整座標軸的顯示範圍
  coord_cartesian(xlim = c(0, 3), ylim = c(0, 20000)) +
  # 更換主題, 這個主題比較簡潔, 也能夠在ggthemes包中獲取其餘主題
  theme_linedraw()

2. 自定義圖片佈局&多種幾何繪圖

library(gridExtra)
#創建數據集
df <- data.frame(
  x = c(3, 1, 5),
  y = c(2, 4, 6),
  label = c("a","b","c")
)  

p <- ggplot(df, aes(x, y, label = label)) +
  # 去掉橫座標信息
  labs(x = NULL, y = NULL) +
  # 切換主題
  theme_linedraw()

p1 <- p + geom_point() + ggtitle("point")
p2 <- p + geom_text() + ggtitle("text")
p3 <- p + geom_bar(stat = "identity") + ggtitle("bar")
p4 <- p + geom_tile() + ggtitle("raster")
p5 <- p + geom_line() + ggtitle("line")
p6 <- p + geom_area() + ggtitle("area")
p7 <- p + geom_path() + ggtitle("path")
p8 <- p + geom_polygon() + ggtitle("polygon")

# 構造ggplot圖片列表
plots <- list(p1, p2, p3, p4, p5, p6, p7, p8)
# 自定義圖片佈局
gridExtra::grid.arrange(grobs = plots, ncol = 4)

3. 箱線圖

統計學中展現數據分散狀況的直觀圖形,在探索性分析中經常用於展現在某個因子型變量下因變量的分散程度。

下面展現箱線圖最長使用的一些方法:app

library(ggplot2) # 繪圖
library(ggsci)   # 使用配色

# 使用diamonds數據框, 分類變量爲cut, 目標變量爲depth
p <- ggplot(diamonds, aes(x = cut, y = carat)) +
  theme_linedraw()

# 一個因子型變量時, 直接用顏色區分不一樣類別, 後面表示將圖例設置在右上角
p1 <- p + geom_boxplot(aes(fill = cut)) + theme(legend.position = "None")
# 兩個因子型變量時, 能夠將其中一個因子型變量設爲x, 將另外一個因子型變量設爲用圖例顏色區分
p2 <- p + geom_boxplot(aes(fill = color)) + theme(legend.position = "None")
# 將箱線圖進行轉置
p3 <- p + geom_boxplot(aes(fill = cut)) + coord_flip() + theme(legend.position = "None")
# 使用現成的配色方案: 包括scale_fill_jama(), scale_fill_nejm(), scale_fill_lancet(), scale_fill_brewer()(藍色系)
p4 <- p + geom_boxplot(aes(fill = cut)) + scale_fill_brewer() + theme(legend.position = "None")

# 構造ggplot圖片列表
plots <- list(p1, p2, p3, p4)
# 自定義圖片佈局
gridExtra::grid.arrange(grobs = plots, ncol = 2)

當研究某個連續型變量的箱線圖涉及多個離散型分類變量時,咱們常使用分面facetting來提升圖表的可視性。python爬蟲

library(ggplot2)

ggplot(diamonds, aes(x = color, y = carat)) +
  # 切換主題
  theme_linedraw() +
  # 箱線圖顏色根據因子型變量color填色
  geom_boxplot(aes(fill = color)) +
  # 分面: 本質上是將數據框按照因子型變量color類劃分爲多個子數據集subset, 在每一個子數據集上繪製相同的箱線圖
  # 注意通常都要加scales="free", 不然子數據集數據尺度相差較大時會被拉扯開
  facet_wrap(~cut, scales="free")

4. 直方圖

library(ggplo2)

# 普通的直方圖
p1 <- ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = cut)) + 
  theme_linedraw() +
  scale_fill_brewer()

# 堆積直方圖
p2 <- ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "identity") + 
  theme_linedraw() +
  scale_fill_brewer()
  
# 累積直方圖
p3 <- ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill") + 
  theme_linedraw() +
  scale_fill_brewer()

# 分類直方圖
p4 <- ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") + 
  theme_linedraw() +
  scale_fill_brewer()

# 構造ggplot圖片列表
plots <- list(p1, p2, p3, p4)
# 自定義圖片佈局
gridExtra::grid.arrange(grobs = plots, ncol = 2)

5. 座標系統

除了前面箱線圖使用的coord_flip()方法實現了座標軸轉置,ggplot還提供了不少和座標系統相關的功能。框架

library(ggplot2)

bar <- ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = cut), show.legend = FALSE, width = 1) + 
  # 指定比率: 長寬比爲1, 便於展現圖形
  theme(aspect.ratio = 1) +
  scale_fill_brewer() +
  labs(x = NULL, y = NULL)

# 座標軸轉置
bar1 <- bar + coord_flip()
# 繪製極座標
bar2 <- bar + coord_polar()

# 構造ggplot圖片列表
plots <- list(bar1, bar2)
# 自定義圖片佈局
gridExtra::grid.arrange(grobs = plots, ncol = 2)

6. 瓦片圖、 熱力圖

機器學習中探索性分析咱們能夠經過corrplot直接繪製全部變量的相關係數圖,用於判斷整體的相關係數狀況。機器學習

library(corrplot)
#計算數據集的相關係數矩陣並可視化
mycor = cor(mtcars)
corrplot(mycor, tl.col = "black")

ggplot提供了更加個性化的瓦片圖繪製:ide

library(RColorBrewer)
# 生成相關係數矩陣
corr <- round(cor(mtcars), 2)
df <- reshape2::melt(corr)
p1 <- ggplot(df, aes(x = Var1, y = Var2, fill = value, label = value)) +
  geom_tile() +
  theme_bw() +
  geom_text(aes(label = value, size = 0.3), color = "white") +
  labs(title = "mtcars - Correlation plot") +
  theme(text = element_text(size = 10), legend.position = "none", aspect.ratio = 1)
p2 <- p1 + scale_fill_distiller(palette = "Reds")
p3 <- p1 + scale_fill_gradient2()
gridExtra::grid.arrange(p1, p2, p3, ncol=3)

更多例子

有經典的50個ggplot2繪圖示例:函數

http://r-statistics.co/Top50-...

其餘文章

1. 機器學習必知必會與算法原理

機器學習導論:什麼是機器學習
機器學習必知必會:凸優化
深刻淺出機器學習算法:XGBoost
機器學習必知必會:梯度降低法

2. 數據分析和爬蟲案例

Python數據分析:誰是2018當之無愧的「第一」國產電影
如何用python爬蟲實現簡單PV刷量——以CSDN爲例
python腳本從零到一構建本身的免費代理IP池

3. 相關經驗

秋招面試:零基礎拿到騰訊數據崗offer須要作哪些努力
股票市場中如何用數據思惟跑贏九成的投資者
精算師證有多難考,怎麼準備?

Reference

[1] https://ggplot2-book.org/intr...
[2] https://rstudio.com/resources...
[3] https://r4ds.had.co.nz/data-v...
[4] https://www.sohu.com/a/320024...

相關文章
相關標籤/搜索