第一章使用ggplot2進(jìn)行數(shù)據(jù)可視化

第一章使用ggplot2進(jìn)行數(shù)據(jù)可視化

library(tidyverse)

查看運(yùn)行環(huán)境

devtools::session_info(c("tidyverse"))

- Session info ---------------------------------------------------------------

setting value

version R version 3.6.1 (2019-07-05)

os Windows 10 x64

system x86_64, mingw32

ui RTerm

language (EN)

collate Chinese (Simplified)_China.936

ctype Chinese (Simplified)_China.936

tz Asia/Taipei

date 2020-05-26

- Packages -------------------------------------------------------------------

package * version date lib source

askpass 1.1 2019-01-13 [1] CRAN (R 3.6.1)

assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1)

backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.1)

base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.6.0)

BH 1.72.0-3 2020-01-08 [1] CRAN (R 3.6.2)

broom 0.5.4 2020-01-27 [1] CRAN (R 3.6.2)

callr 3.4.1 2020-01-24 [2] CRAN (R 3.6.2)

cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.2)

cli 2.0.1 2020-01-08 [1] CRAN (R 3.6.2)

clipr 0.7.0 2019-07-23 [2] CRAN (R 3.6.1)

colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.1)

crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1)

curl 4.3 2019-12-02 [1] CRAN (R 3.6.2)

DBI 1.1.0 2019-12-15 [2] CRAN (R 3.6.2)

dbplyr 1.4.2 2019-06-17 [1] CRAN (R 3.6.2)

digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.2)

dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.1)

ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.1)

evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.3)

fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2)

farver 2.0.3 2020-01-16 [1] CRAN (R 3.6.2)

forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.6.2)

fs 1.3.1 2019-05-06 [2] CRAN (R 3.6.1)

generics 0.0.2 2018-11-29 [2] CRAN (R 3.6.1)

ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.1)

glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.1)

gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.1)

haven 2.2.0 2019-11-08 [1] CRAN (R 3.6.2)

highr 0.8 2019-03-20 [1] CRAN (R 3.6.3)

hms 0.5.3 2020-01-08 [1] CRAN (R 3.6.2)

htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.1)

httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1)

jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.1)

knitr 1.28 2020-02-06 [1] CRAN (R 3.6.3)

labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)

lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.1)

lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.1)

lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.1)

lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.2)

magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1)

markdown 1.1 2019-08-07 [1] CRAN (R 3.6.3)

MASS 7.3-51.4 2019-03-31 [2] CRAN (R 3.6.1)

Matrix 1.2-17 2019-03-22 [2] CRAN (R 3.6.1)

mgcv 1.8-31 2019-11-09 [2] CRAN (R 3.6.2)

mime 0.8 2019-12-19 [1] CRAN (R 3.6.2)

modelr 0.1.5 2019-08-08 [1] CRAN (R 3.6.2)

munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.1)

nlme 3.1-140 2019-05-12 [2] CRAN (R 3.6.1)

openssl 1.4.1 2019-07-18 [1] CRAN (R 3.6.1)

pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2)

pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1)

plogr 0.2.0 2018-03-25 [1] CRAN (R 3.6.1)

plyr 1.8.5 2019-12-10 [1] CRAN (R 3.6.2)

prettyunits 1.1.1 2020-01-24 [2] CRAN (R 3.6.2)

processx 3.4.1 2019-07-18 [2] CRAN (R 3.6.1)

progress 1.2.2 2019-05-16 [2] CRAN (R 3.6.1)

ps 1.3.0 2018-12-21 [2] CRAN (R 3.6.1)

purrr * 0.3.3 2019-10-18 [1] CRAN (R 3.6.2)

R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.2)

RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.6.0)

Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.1)

readr * 1.3.1 2018-12-21 [1] CRAN (R 3.6.2)

readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.2)

rematch 1.0.1 2016-04-21 [1] CRAN (R 3.6.2)

reprex 0.3.0 2019-05-16 [1] CRAN (R 3.6.2)

reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.6.1)

rlang 0.4.5 2020-03-01 [1] CRAN (R 3.6.3)

rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.1)

rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.3)

rvest 0.3.5 2019-11-08 [1] CRAN (R 3.6.2)

scales 1.1.0 2019-11-18 [1] CRAN (R 3.6.2)

selectr 0.4-2 2019-11-20 [1] CRAN (R 3.6.2)

stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0)

stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.6.1)

sys 3.3 2019-08-21 [1] CRAN (R 3.6.1)

tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.1)

tidyr * 1.0.0 2019-09-11 [1] CRAN (R 3.6.1)

tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.6.3)

tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 3.6.2)

tinytex 0.19 2020-01-14 [1] CRAN (R 3.6.2)

utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.1)

vctrs 0.2.99.9002 2020-01-13 [1] Github (r-lib/vctrs@038b6af)

viridisLite 0.3.0 2018-02-01 [1] CRAN (R 3.6.1)

whisker 0.4 2019-08-28 [2] CRAN (R 3.6.1)

withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1)

xfun 0.12 2020-01-13 [1] CRAN (R 3.6.3)

xml2 1.2.2 2019-08-09 [2] CRAN (R 3.6.1)

yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)

[1] C:/Users/wlx/Documents/R/win-library/3.6

[2] C:/Program Files/R/R-3.6.1/library

ggplot2::mpg libray之后就不用這樣了

1.mpg數(shù)據(jù)框

數(shù)據(jù)框是變量(列)和觀測(cè)(行)的矩形集合。mpg是ggplot2的內(nèi)置數(shù)據(jù)框。

拿到一個(gè)數(shù)據(jù)首先就要觀察它!忘了誰(shuí)說(shuō)的反正好有道理。

Fuel economy data from 1999 and 2008 for 38 popular models of car

234 行x 11列

1.manufacturer:生產(chǎn)商 15個(gè)

2.model:型號(hào) 38個(gè)

3.displ:引擎排量-L 35個(gè),單位為升,小數(shù)

4.year:出廠年份

5.cly:汽缸數(shù) 4,5,6,8

6.trans:變速方式:10個(gè)

7.dry:驅(qū)動(dòng)方式 f r 4

8.cty :每加侖汽油能跑的公里數(shù)(城市)21個(gè),整數(shù)

9.hwy:燃油效率:每加侖汽油能跑的公里數(shù)(高速路)單位英里/加侖,燃油效率高說(shuō)明省油。 27個(gè),整數(shù)。

10.fl:燃油類型,五個(gè) p r e d c

11.class:車型 七個(gè) compact midsize suv 2seater minivan pickup subcompact

library(ggplot2) ggplot包含在tidyverse中,不用再單獨(dú)library

mpg

# A tibble: 234 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(l~ 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(a~ f 21 30 p comp~

5 audi a4 2.8 1999 6 auto(l~ f 16 26 p comp~

6 audi a4 2.8 1999 6 manual~ f 18 26 p comp~

7 audi a4 3.1 2008 6 auto(a~ f 18 27 p comp~

8 audi a4 quat~ 1.8 1999 4 manual~ 4 18 26 p comp~

9 audi a4 quat~ 1.8 1999 4 auto(l~ 4 16 25 p comp~

10 audi a4 quat~ 2 2008 4 manual~ 4 20 28 p comp~

# ... with 224 more rows

?mpg #查看幫助文檔

用dplyr包的distinct函數(shù)

p<-mpg
library(dplyr)
distinct(p,manufacturer) #manufacturer替換為其他列名。僅顯示非重復(fù)值,不顯示重復(fù)次數(shù)。

# A tibble: 15 x 1

manufacturer

<chr>

1 audi

2 chevrolet

3 dodge

4 ford

5 honda

6 hyundai

7 jeep

8 land rover

9 lincoln

10 mercury

11 nissan

12 pontiac

13 subaru

14 toyota

15 volkswagen

count(p,manufacturer) #顯示出現(xiàn)次數(shù)

# A tibble: 15 x 2

manufacturer n

<chr> <int>

1 audi 18

2 chevrolet 19

3 dodge 37

4 ford 25

5 honda 9

6 hyundai 14

7 jeep 8

8 land rover 4

9 lincoln 3

10 mercury 4

11 nissan 13

12 pontiac 5

13 subaru 14

14 toyota 34

15 volkswagen 27

count(p,class) #顯示出現(xiàn)次數(shù)

# A tibble: 7 x 2

class n

<chr> <int>

1 2seater 5

2 compact 47

3 midsize 41

4 minivan 11

5 pickup 33

6 subcompact 35

7 suv 62

count(p,drv)

# A tibble: 3 x 2

drv n

<chr> <int>

1 4 103

2 f 106

3 r 25

count(p,cyl)

# A tibble: 4 x 2

cyl n

<int> <int>

1 4 81

2 5 4

3 6 79

4 8 70

基礎(chǔ)作圖

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))


image.png

加顏色

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))

image.png

大小

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))


image.png

透明度和形狀

將車型class映射給透明度

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))

image.png

將車型class映射給形狀

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))

image.png

(4)手動(dòng)設(shè)置圖形屬性

例:所有點(diǎn)設(shè)為藍(lán)色

(注意:color="blue"在aes()外)

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")


image.png

(5)stroke-輪廓(筆者補(bǔ)充)

適用于散點(diǎn)圖,21-24號(hào)形狀

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, stroke = 3),shape=21)

image.png

5.分面

(1)依據(jù)單個(gè)變量分面 facet_wrap() #分子圖

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2) #分兩行展示

image.png

nrow指定分面后顯示幾行

ncol指定分面后顯示幾列

注意~分面依據(jù)必須是離散型變量。

(2)依據(jù)兩個(gè)變量分面 facet_grid()

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)

image.png

不需要指定nrow和ncol。

(3)不想在行或列維度中分面,用.代替變量名

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)


image.png

6.幾何對(duì)象

也就是圖的不同類型,如點(diǎn)圖、折線圖、直方圖等。

(1)理解分組

將一個(gè)圖形屬性映射為一個(gè)離散型變量,ggplot2就會(huì)自動(dòng)對(duì)數(shù)據(jù)進(jìn)行分組來(lái)繪制多個(gè)幾何對(duì)象。這種形式是隱式分組,不需要添加圖例和區(qū)分特征。

ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))


image.png

將線性映射為drv(驅(qū)動(dòng)方式,d,f,4)就會(huì)自動(dòng)變成三條線型不同的線。

將顏色映射為drv,就會(huì)自動(dòng)變成三條顏色不用的線。

分組

ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))


image.png

隱式分組-線型

ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, linetype = drv),
)

image.png

隱式分組-顏色

ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE #不顯示圖例
)

image.png

(2)同一張圖顯示多個(gè)幾何對(duì)象--局部映射和全局映射

--這里涉及到圖層啦。

局部映射-映射只對(duì)改圖層有效

有多個(gè)幾何對(duì)象時(shí),映射語(yǔ)句要重復(fù)多次,又丑又麻煩。

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))


image.png

全局映射--對(duì)所有圖層生效

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()

image.png

局部映射與全局映射沖突時(shí),服從局部映射。

例如:

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)

image.png

如果出現(xiàn)報(bào)錯(cuò),請(qǐng)library(dplyr) 或library(tidyverse)

Error in class == "subcompact" : comparison (1) is possible only for atomic and list types

這個(gè)報(bào)錯(cuò)是因?yàn)閒ilter函數(shù)出自dplyr包,不加載就不能用~

ps:關(guān)于se=FALSE

se是standard error的縮寫(xiě),se參數(shù)為擬合曲線添加標(biāo)準(zhǔn)誤差帶,也就是那個(gè)灰不啦嘰的灰色背景帶,默認(rèn)是TRUE。

7.統(tǒng)計(jì)變換

(1)關(guān)于diamonds數(shù)據(jù)集 (筆者補(bǔ)充)

ggplot2內(nèi)置數(shù)據(jù)集,包含53940顆鉆石的信息。

carat:克拉

cut:切割質(zhì)量

color:顏色等級(jí)

clarity:純凈度等級(jí)

depth:深度比例

table:鉆石頂部相對(duì)于最寬點(diǎn)的寬度

price:價(jià)格

"x" "y" "z" :長(zhǎng)寬深

↑以上來(lái)自幫助文檔?diamonds

(2)統(tǒng)計(jì)變換函數(shù)和幾何對(duì)象函數(shù)

統(tǒng)計(jì)變換:繪圖時(shí)用來(lái)計(jì)算新數(shù)據(jù)的算法叫做統(tǒng)計(jì)變換stat

geom_bar做出的圖縱坐標(biāo)為count,是計(jì)算的新數(shù)據(jù)。

geom_bar的默認(rèn)統(tǒng)計(jì)變換是stat_count,stat_count會(huì)計(jì)算出兩個(gè)新變量-count(計(jì)數(shù))和prop(proportions,比例)。

每個(gè)幾何對(duì)象函數(shù)都有一個(gè)默認(rèn)的統(tǒng)計(jì)變換,每個(gè)統(tǒng)計(jì)變換函數(shù)都又一個(gè)默認(rèn)的幾何對(duì)象。

用幾何對(duì)象函數(shù)geom_bar作直方圖,默認(rèn)統(tǒng)計(jì)變換是stat_count,

diamonds

# A tibble: 53,940 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

7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47

8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53

9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49

10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39

# ... with 53,930 more rows

count(diamonds,cut) #顯示出現(xiàn)次數(shù)

# A tibble: 5 x 2

cut n

<ord> <int>

1 Fair 1610

2 Good 4906

3 Very Good 12082

4 Premium 13791

5 Ideal 21551

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))


image.png

用統(tǒng)計(jì)變換函數(shù)stat_count做計(jì)數(shù)統(tǒng)計(jì)圖,默認(rèn)幾何對(duì)象是直方圖。

ggplot(data [圖片上傳中...(image.png-18999c-1590543012090-0)]
= diamonds) +
stat_count(mapping = aes(x = cut))


image.png

這兩個(gè)代碼做出的圖片結(jié)果是一致的。兩種方法沒(méi)有優(yōu)劣之分。

(3)顯示使用某種統(tǒng)計(jì)變換的原因

覆蓋默認(rèn)的統(tǒng)計(jì)變換

直方圖默認(rèn)的統(tǒng)計(jì)變換是stat_count,也就是統(tǒng)計(jì)計(jì)數(shù)。當(dāng)需要直接用原表格的數(shù)據(jù)作圖時(shí)就會(huì)需要覆蓋默認(rèn)的。

demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
) #新建表格并賦值給demo

ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity") #覆蓋默認(rèn)的統(tǒng)計(jì)變換,使用identity。

image.png

覆蓋從統(tǒng)計(jì)變換生成變量到圖形屬性的默認(rèn)映射

直方圖默認(rèn)的y軸是x軸的計(jì)數(shù)。此例子中x軸是是五種cut(切割質(zhì)量),直方圖自動(dòng)統(tǒng)計(jì)了這五種質(zhì)量的鉆石的統(tǒng)計(jì)計(jì)數(shù),當(dāng)你不想使用計(jì)數(shù),而是想顯示各質(zhì)量等級(jí)所占比例的時(shí)候就需要用到prop。

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

image.png

這里group=1的意思是把所有鉆石作為一個(gè)整體,顯示五種質(zhì)量的鉆石所占比例體現(xiàn)出來(lái)。如果不加這一句,就是每種質(zhì)量的鉆石各為一組來(lái)計(jì)算,那么比例就都是100%,顯示五根大黑柱子,實(shí)在是丑出新高度。

在代碼中強(qiáng)調(diào)統(tǒng)計(jì)變換

以stat_summary為例。

ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)

image.png

(小潔碎碎念:stat_summary的默認(rèn)幾何圖形是geom_pointrange,而這個(gè)geom_pointrange默認(rèn)的統(tǒng)計(jì)變換卻是identity,如果你不知道其中貓膩,就會(huì)發(fā)現(xiàn)他倆代碼竟然不可逆。。。一夫多妻的節(jié)奏呀。)

因此要用幾何對(duì)象函數(shù)重復(fù)這個(gè)圖形,則需要指定stat_summary。

ggplot(data = diamonds) +
geom_pointrange(
mapping = aes(x = cut, y = depth),
stat = "summary",
fun.ymin = min,
fun.ymax = max,
fun.y = median
)

image.png

8.位置調(diào)整-position

在直方圖中,顏色映射是由color和fill之分的,表示邊框和填充。如果要設(shè)置無(wú)填充(也就是透明),則fill=NA。NA在數(shù)據(jù)框里表示空值。

(1)直方圖之堆疊式-fill

堆疊式就是在基礎(chǔ)條形圖上添加第三個(gè)變量,將這個(gè)變量映射給fill,就會(huì)在每個(gè)條形中分出不同顏色且不同比例的矩形。

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))

image.png

除了映射的方式以外,position參數(shù)設(shè)置位置調(diào)整功能。position="fill"也可以設(shè)置,但這樣設(shè)置的每組堆疊條形具有相同的高度。

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")

image.png

筆者補(bǔ)充:感覺(jué)這種堆疊方式并不如設(shè)置fill映射,因?yàn)樗怀龅氖潜壤?,每組之間數(shù)值的差異被忽略了。

(2)直方圖之對(duì)象直接顯示-identity

ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
geom_bar(alpha = 1/5, position = "identity")


image.png

ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity")


image.png

書(shū)中23頁(yè)identity設(shè)置透明度和無(wú)填充的圖是錯(cuò)的!

正確的是這樣

(3)直方圖之并列式-dodge

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")


image.png

(4)散點(diǎn)圖之?dāng)_動(dòng)-jitter

書(shū)中翻譯為抖動(dòng),我認(rèn)為擾動(dòng)更精確。

以mpg的displ和hwy散點(diǎn)圖為例

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))


image.png

在這個(gè)例子中,數(shù)據(jù)有234行,圖中卻只有126個(gè)點(diǎn)。這就是因?yàn)橛行c(diǎn)是重疊的,圖上雖然顯示一個(gè)點(diǎn),但其實(shí)是好幾個(gè)點(diǎn)重疊成了一個(gè)。

jitter可以為點(diǎn)添加隨機(jī)擾動(dòng),使重疊的點(diǎn)分散開(kāi)。

position參數(shù)設(shè)為jitter的快速實(shí)現(xiàn):geom_jitter()

除了geom_jitter外,geom_point也可以展示重疊點(diǎn),會(huì)根據(jù)重疊點(diǎn)的個(gè)數(shù)調(diào)整大小。

(5)stack-堆疊 無(wú)

ggplot(series, aes(time, value, group = type)) +

geom_line(aes(colour = type), position = "stack") +

geom_point(aes(colour = type), position = "stack")

ggplot(series, aes(time, value, group = type)) +

geom_line(aes(colour = type)) +

geom_point(aes(colour = type))

設(shè)置position_stack(上)和不設(shè)置(下)的區(qū)別:

9.坐標(biāo)系

(1)coord_flip翻轉(zhuǎn)坐標(biāo)系

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()


image.png

(2)coord_quickmap

為地圖設(shè)置長(zhǎng)寬比

此處需要加載maps包,否則會(huì)報(bào)錯(cuò)。

library(maps)

如果報(bào)錯(cuò)則:install.packages("maps")

library(maps)

nz <- map_data("nz")

ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")


image.png

ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
coord_quickmap()


image.png

geom_polygon 是多邊形圖

(3)coord_polar 極坐標(biāo)系

bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()

image.png

bar + coord_polar()

image.png

ps:習(xí)題中涉及的

(1)關(guān)于餅圖/牛眼圖/圓圈圖

ggplot(mpg, aes(x = factor(1), fill = drv)) +
geom_bar()

image.png

ggplot(mpg, aes(x = factor(1), fill = drv)) +
geom_bar(width = 1) +
coord_polar(theta = "y")

image.png

要點(diǎn):

不分組,只有一個(gè)因子型變量drv。

如果作圖不設(shè)置width,餅圖中間會(huì)出現(xiàn)一個(gè)白色圈圈。經(jīng)測(cè)試發(fā)現(xiàn)width等于幾在圖上并沒(méi)有區(qū)別,但是不設(shè)置卻不行。

theta是角度的意思。如果不設(shè)置這個(gè)參數(shù)就會(huì)得到牛眼圖哈哈哈哈哈哈。

作者取名叫牛眼圖

多圓圈圖

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill") +
coord_polar(theta = "y")


image.png

(2)第三題

geom_abline:添加線條

coord_fixed:保證橫縱坐標(biāo)的標(biāo)尺一致,線條呈45°角

10.完整的繪圖模板

ggplot(data = <DATA>) +

<GEOM_FUNCTION>(

mapping = aes(<MAPPINGS>),

stat = <STAT>,

position = <POSITION>

) +

<COORDINATE_FUNCTION> +

<FACET_FUNCTION>

圖形構(gòu)建的過(guò)程

(1)數(shù)據(jù)集

(2)統(tǒng)計(jì)變換

(3)幾何對(duì)象

(4)映射fill

(5)放置

(6)映射x/y

參考
詳解《R數(shù)據(jù)科學(xué)》--第一章ggplot2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容