ggplot2:第五章-工具箱(一)

Date: 27th september 2019
-本章內(nèi)容較多,其相對(duì)較難,因此,我們將會(huì)分為幾部分進(jìn)行記錄。

5.1 簡(jiǎn)介(略)

5.2 圖層疊加的總體策略

圖層有三種用途
1)用以展示數(shù)據(jù)本身
2)用以展示數(shù)據(jù)的統(tǒng)計(jì)摘要
3)用以添加額外的元數(shù)據(jù)

5.3 基本圖形類(lèi)型

geom_area() 用于繪制面積圖
geom_bar() 用于繪制條形圖
geom_line() 用于繪制線(xiàn)條圖
geom_point() 用于繪制散點(diǎn)圖
geom_polygon() 用于繪制多邊圖
geom_text() 可以指定點(diǎn)添加標(biāo)簽。他是唯一一個(gè)額外圖形屬性的:它需指定label參數(shù)。可以通過(guò)設(shè)置可選的圖形屬性hjust和vjust來(lái)控制文本的橫縱位置。
geom_tile() 用來(lái)繪制色深圖(image plot)或水平圖(level plot)
繪制以上幾種幾何圖形

df <- data.frame(
  x = c(3,1,5),
  y = c(2,4,6),
  label = c("a", "b", "c")
)
p <- ggplot(df, aes(x,y)) + xlab(NULL) + ylab(NULL)
p + geom_point() + labs(title = "geom_point")  #散點(diǎn)圖
p + geom_bar(stat = "identity") + labs(title = "geom_bar(stat=\"identity\")") #直方圖
p + geom_line() + labs(title = "geom_line")  #線(xiàn)性圖
p + geom_area() + labs(title = "geom_area")  #面積
p + geom_path() + labs(title = "geom_path")  #路徑圖
p + geom_text(aes(label =label)) + labs(title = "geom_text")  #添加標(biāo)簽
p + geom_tile() + labs(title = "geom_tile") #深色圖
p + geom_polygon() + labs(title = "geom_polygon")  #多邊形
bar_map

line_map

5. 4 展示數(shù)據(jù)分布

改變組距寬度(binwidth)或顯示精確指定切分位置(breadks)
有多種方式可以用來(lái)進(jìn)行分布的跨組比較:同時(shí)繪制多個(gè)小的直方圖,facets = . ~var;使用頻率多邊形(frequency polygon),geom = "freqploly";或者使用條件密度圖, position = "fill"

depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58,58)
depth_dist + geom_histogram(aes(y = ..densitiy..), binwidth = 0.1) + 
  facet_grid(cut ~.)

depth_dist +geom_histogram(aes(fill = cut ), binwidth = 0.1, position = "fill")
depth_dist + geom_freqpoly(aes(y = ..dendity.., colour = cut), 
                           binwidth = 0.1)

作為幾何對(duì)象的直方圖和頻率多邊形均使用stat_bin統(tǒng)計(jì)變換。次統(tǒng)計(jì)變換生成了兩個(gè)輸出變量count和density。變量count為默認(rèn)值

geom_boxplot = stat_boxplot +geom_boxplot #箱線(xiàn)圖,即一個(gè)連續(xù)型變量針對(duì)一個(gè)類(lèi)別變量條件所得的圖形

箱線(xiàn)圖也可以對(duì)連續(xù)變量取條件,前提是數(shù)據(jù)預(yù)先經(jīng)過(guò)巧妙的封箱(binning)處理

箱線(xiàn)圖

library(plyr)
qplot(cut, deoth, data = diamonds, geom = "boxplot")
qplot(carat, depth, data = diamonds, geom = "boxplot", 
      group = round_any(carat, 0.1,floor), xlim = c(0,3))
#圖解釋?zhuān)簩?duì)于連續(xù)型變量,必須設(shè)置group圖形屬性以得到多個(gè)箱線(xiàn)圖。此處使用了group = round_any(carat, 0.1, floor)
####來(lái)獲得針對(duì)變量carat以0,1單位為大小封箱后的箱線(xiàn)圖
boxplot

geom_jitter = position_jitter + geom_jitter :通過(guò)在離散型分布 上添加隨機(jī)噪聲以避免遮蓋繪制完問(wèn)題,這是一種較為粗糙的方法。

qplot(class, cty, data = mpg, geom = "jitter")
qplot(class, drv, data = mpg, geom = "jitter")

geom_density = stat_density + geom_area ##基于核平滑方法進(jìn)行平滑后得到頻率多邊形。 僅在已知潛在的密度分布為平滑、連續(xù)且無(wú)界的時(shí)候使用這種密度圖。

qplot(depth, data = diamonds, geom = "density", xlim = c(54, 70)) #變量depth的密度圖
qplot(depth, data = diamonds, geom = "density", xlim = c(54, 70),
      fill = cut, alpha = I(0.2))  #按照變量cut的不同取值上色

5.5 處理遮蓋繪制問(wèn)題

散點(diǎn)圖是研究?jī)蓚€(gè)連續(xù)型變量間關(guān)系的重要工具, 但是數(shù)據(jù)量很大時(shí),這些點(diǎn)出現(xiàn)重疊問(wèn)題,從而掩蓋真實(shí)關(guān)系,這種為為“遮蓋繪制(overplotting)”

#下圖所用數(shù)據(jù)從兩個(gè)獨(dú)立的正態(tài)分布中所得的2000個(gè)點(diǎn),
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
nrom <- ggplot(df, aes(x, y))
norm + geom_point()
norm + geom_point(shaoe = 1)
norm + geom_point(shape = ".")
###使用"alpha"進(jìn)行透明度的設(shè)置
norm + geom_point(colour = "black", alpha = 1/3)

##數(shù)據(jù)離散性    設(shè)置兩個(gè)連續(xù)整數(shù)間距的一半(0.5)作為打散的寬度
td <- ggplot(diamonds, aes(table, depth)) + 
  xlim(50, 70) + ylim(50, 70)
td + geom_point()
td + geom_jitter()
td +geom_jitter(width = 0.5, alpha = 1/10) ##alpha 參數(shù)調(diào)節(jié)透明度
td + geom_jitter(position = jit)
td + geom_jitter(position = jit, colour = "black", alpha = 1/10)  #調(diào)節(jié)透明度
point_map

jitter_map

第五章的后半部分內(nèi)容將在一節(jié)進(jìn)行描述

ggplot2: 第二章 -全網(wǎng)最全的教程
ggplot2:第三章-語(yǔ)法突破
ggplot2:第四章-用圖層構(gòu)建圖像
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ù)。

相關(guān)閱讀更多精彩內(nèi)容

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