關于diamonds數(shù)據(jù)集
ggplot2內(nèi)置數(shù)據(jù)集,包含53940顆鉆石的信息。
carat:克拉
cut:切割質(zhì)量
color:顏色等級
clarity:純凈度等級
depth:深度比例
table:鉆石頂部相對于最寬點的寬度
price:價格
"x" "y" "z" :長寬深
↑以上來自幫助文檔?diamonds

統(tǒng)計變換函數(shù)和幾何對象函數(shù)
統(tǒng)計變換:繪圖時用來計算新數(shù)據(jù)的算法叫做統(tǒng)計變換stat
geom_bar做出的圖縱坐標為count,是計算的新數(shù)據(jù)。
geom_bar的默認統(tǒng)計變換是stat_count,stat_count會計算出兩個新變量-count(計數(shù))和prop(proportions,比例)。
每個幾何對象函數(shù)都有一個默認的統(tǒng)計變換,每個統(tǒng)計變換函數(shù)都又一個默認的幾何對象。
如:用幾何對象函數(shù)geom_bar作直方圖,默認統(tǒng)計變換是stat_count,
用統(tǒng)計變換函數(shù)stat_count做計數(shù)統(tǒng)計圖,默認幾何對象是直方圖。


這兩者是等價的

為什么要使用某種統(tǒng)計變換?
1.覆蓋默認的統(tǒng)計變換
直方圖默認的統(tǒng)計變換是stat_count,也就是統(tǒng)計計數(shù)。當需要直接用原表格的數(shù)據(jù)作圖時就會需要覆蓋默認的。
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")
這里直接用頻數(shù)表作圖不需要再統(tǒng)計變換了,于是用stat = "identity"覆蓋默認的統(tǒng)計變換

2.Y軸可以用比例來表示
直方圖默認的y軸是x軸的計數(shù)。此例子中x軸是是五種cut(切割質(zhì)量),直方圖自動統(tǒng)計了這五種質(zhì)量的鉆石的統(tǒng)計計數(shù),當你不想使用計數(shù),而是想顯示各質(zhì)量等級所占比例的時候就需要用到prop。
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

注意:1. y = ..prop..;
2.group=1的意思是把所有鉆石作為一個整體,顯示五種質(zhì)量的鉆石所占比例體現(xiàn)出來。如果不加這一句,就是每種質(zhì)量的鉆石各為一組來計算,那么比例就都是100%,顯示五根大黑柱子。
3.在代碼中強調(diào)統(tǒng)計變換。
以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的默認幾何圖形是geom_pointrange,而這個geom_pointrange默認的統(tǒng)計變換卻是identity,如果你不知道其中貓膩,就會發(fā)現(xiàn)他倆代碼竟然不可逆。。。一夫多妻的節(jié)奏呀。)
因此要用幾何對象函數(shù)重復這個圖形,則需要指定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
位置調(diào)整-position
在直方圖中,顏色映射是由color和fill之分的,表示邊框和填充。如果要設置無填充(也就是透明),則fill=NA。NA在數(shù)據(jù)框里表示空值。
1.直方圖之堆疊式-fill
堆疊式就是在基礎條形圖上添加第三個變量,將這個變量映射給fill,就會在每個條形中分出不同顏色且不同比例的矩形。
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut,fill=clarity))image.png
除了映射的方式以外,position參數(shù)設置位置調(diào)整功能。position="fill"也可以設置,但這樣設置的每組堆疊條形具有相同的高度。
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")image.png
筆者補充:感覺這種堆疊方式并不如設置fill映射,因為他突出的是比例,每組之間數(shù)值的差異被忽略了。
2.直方圖之對象直接顯示-identity


這個直方圖是重疊的
3.直方圖之并列式-dodge
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")


3.散點圖之擾動-jitter
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

添加擾動
position參數(shù)設為jitter的快速實現(xiàn):geom_jitter()
ggplot(data = mpg) + geom_jitter(mapping = aes(x = displ, y = hwy))

4.stack-堆疊

三、坐標系
(1)coord_flip#翻轉(zhuǎn)坐標系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()
(2)coord_quickmap#為地圖設置長寬比此處需要加載maps包,否則會報錯。
nz <- map_data("nz")
ggplot(nz, aes(long, lat, group = group)) + geom_polygon(fill = "white", colour = "black")#geom_polygon 是多邊形圖
ggplot(nz, aes(long, lat, group = group)) + geom_polygon(fill = "white", colour = "black") + coord_quickmap()
(3)coord_polar #極坐標系
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()
bar + coord_polar()




