《ggplot2:數據分析與圖形藝術》,即ggplot2: Elegant Graphics for Data Analysis,目前網上介紹的比較多的是第二版且已經有中文版,Hadley Wickham等目前已經更新到了第三版,做了很多調整如下:
- 在線地址:http://ggplot2-book.org/.
- 在線地址:https://ggplot2.tidyverse.org/index.html
- The chapters on data analysis and modelling have been removed. You can find updated versions of this material in R for Data Science at https://r4ds.had.co.nz.
- The toolbox chapter has been split into multiple chapters.
- New chapter on arranging multiple plots on the page.
- The positioning chapter has been split into facetting and coordinate systems.
- New FAQ chapter that covers some of the most commonly seen problems in the wild.
- The scales and guides chapters have been radically reconfigured into one chapter each on position, colour, and other scales and guides, and then one chapter that focusses on the underlying theory.

全書主要分為三個部分:第一部分初步介紹ggplot2的相關概念,並提供了新手可能遇到的問題的解決辦法。
第二部分從繪圖語法入手(the grammar of graphics),關鍵點在於圖層疊加,進行可視化結果的細節調整和美化。
第三部分是有關數據分析的相關內容,實現數據分析與可視化的結合,這一部分內容與Hadley Wickham的另一本書R for Data Science有重合的地方,可以選擇跳過。

安裝ggplot2包:
install.packages("ggplot2")
安裝完成后,可用“library()”檢查是否安裝。

ggplot2安裝完成后,會有內置的數據集,它是默認繪圖的基礎數據,我們來看看是哪些
library('ggplot2')
data(package = 'ggplot2') #查看ggplot2內置數據集

Data sets in package ‘ggplot2’:
diamonds Prices of over 50,000 round cut diamonds
economics US economic time series
economics_long US economic time series
faithfuld 2d density estimate of Old Faithful data
luv_colours 'colors()' in Luv space
midwest Midwest demographics
mpg Fuel economy data from 1999 to 2008 for 38
popular models of cars
msleep An updated and expanded version of the
mammals sleep dataset
presidential Terms of 11 presidents from Eisenhower to
Obama
seals Vector field of seal movements
txhousing Housing sales in TX
本文使用mpg數據集,重點介紹:
瀏覽下mpg數據
> head(mpg)#看下數據集的前幾列
# A tibble: 6 x 11 manufacturer model displ year cyl trans drv cty hwy fl class <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p comp~ 2 audi a4 1.8 1999 4 manual(~ f 21 29 p comp~ 3 audi a4 2 2008 4 manual(~ f 20 31 p comp~ 4 audi a4 2 2008 4 auto(av) f 21 30 p comp~ 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p comp~ 6 audi a4 2.8 1999 6 manual(~ f 18 26 p c
整個數據集有234行 x 11列

mpg數據集每個變量簡介
manufacturer 生產廠家,如奧迪audi、吉普jeep等 model model name 車型,如奧迪A4、奧迪A6等 displ engine displacement, in litres,發動機排量,單位為每升 year year of manufacture,出廠年份 cyl number of cylinders,氣缸數量 trans type of transmission,傳輸類型,手動還是自動 drv f = front-wheel drive, r = rear wheel drive, 4 = 4wd,驅動類型 ,前輪還是后輪驅動 cty city miles per gallon,每加侖油城市駕駛里程數 hwy highway miles per gallon,每加侖油高速駕駛里程數 fl fuel type,燃油型號 class "type" of car,suv、桑塔納等
ggplot2 繪圖必備三要素
- 數據集(data)
- 圖像屬性(aes)
- 幾何對象(geom)
library('ggplot2')
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
ggplot(mpg, #數據集(data)
aes(x = displ, y = hwy)) + #圖像屬性(aes),即指定x、y軸要投影的數據 data和aes通過ggplot組合在一起,使用+號添加圖層(layers)
geom_point() #幾何對象(geom),此處為散點圖

幾乎每個plot都會將一個變量映射到x和y,因此命名這些參數非常繁瑣,因此aes()的前兩個未命名參數將映射到x和y。這意味着以下代碼與上面的示例相同:
ggplot(mpg, aes(displ, hwy)) + geom_point()
aes設置 (Colour, size, shape and other aesthetic attributes)
按變量class分類繪制分類散點圖
library('ggplot2')
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()

按變量drv不同繪制不同類不同shape散點圖
library('ggplot2')
ggplot(mpg, aes(displ, hwy, shape=drv)) +
geom_point()

按變量cyl值大小繪制不同類不同size散點圖
library('ggplot2')
ggplot(mpg, aes(displ, hwy, size=cyl)) +
geom_point()

如果要將美學設置為固定值,而不縮放它,請在aes()之外的單個層中執行此操作。比較以下兩個圖:
ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = "blue")) ggplot(mpg, aes(displ, hwy)) + geom_point(colour = "blue")


在第一個圖中,值“藍色”被縮放為粉紅色,並添加了一個圖例。在第二個圖中,點被賦予藍色。
不同類型的審美屬性在不同類型的變量下效果更好。例如,顏色和形狀適用於分類變量,而大小適用於連續變量。數據的數量也很重要:如果有大量的數據,就很難區分不同的群體。
當在情節中使用美學時,少通常是多。很難同時看到顏色、形狀和大小之間的關系,所以在使用美學時要克制。不要試圖把一個非常復雜的情節一下子展現出來,看看你能否創造出一系列簡單的情節來講述一個故事,引導讀者從無知走向知識。
