拿R來畫畫(五):全面掌握條形圖

一一對應版

library(gcookbook)
library(ggplot2)
head(pg_mean)
A data.frame: 3 × 2
groupweight
<fct><dbl>
ctrl5.032
trt14.661
trt25.526
ggplot(pg_mean, aes(x = group, y = weight)) + 
geom_bar(stat = 'identity')

在這裏插入圖片描述

改變填充色和邊框色

ggplot(pg_mean, aes(x = group, y = weight)) + 
geom_bar(stat = 'identity', fill = 'lightblue', color = 'green')

在這裏插入圖片描述

簇狀條形圖

head(cabbage_exp)
A data.frame: 6 × 6
CultivarDateWeightsdnse
<fct><fct><dbl><dbl><int><dbl>
c39d163.180.9566144100.30250803
c39d202.800.2788867100.08819171
c39d212.740.9834181100.31098410
c52d162.260.4452215100.14079141
c52d203.110.7908505100.25008887
c52d211.470.2110819100.06674995
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(position = 'dodge', stat = 'identity')

在這裏插入圖片描述

  • geom_bar中的width位於0到1之間,默認爲0.9,可以通過調整這個來調整寬度。
  • 通過令position_dodge的值大於width來調整組內條形圖之間的距離,否則就會重疊一部分
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(stat = 'identity', width = 0.4, position = position_dodge(0.3))

在這裏插入圖片描述

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(stat = 'identity', width = 0.4, position = position_dodge(0.5))

在這裏插入圖片描述

不加dodge條件就會生成堆積條形圖

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(stat = 'identity')

在這裏插入圖片描述

scale_fill_manual可以手動調整顏色

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(position = 'dodge', stat = 'identity') + 
scale_fill_manual(values = c('#8DD35F','#FFE680'))

在這裏插入圖片描述

分別在柱子頂端的上下位置加圖例

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(position = 'dodge', stat = 'identity') + 
scale_fill_manual(values = c('#8DD35F','#FFE680')) + 
geom_text(aes(label = Weight), vjust = -0.5, position = position_dodge(0.9), size = 4, color = 'green')

在這裏插入圖片描述

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + 
geom_bar(position = 'dodge', stat = 'identity') + 
scale_fill_manual(values = c('#8DD35F','#FFE680')) + 
geom_text(aes(label = Weight), vjust = 1.5, position = position_dodge(0.9), size = 5, color = 'white')

在這裏插入圖片描述

頻數條形圖

Note the documentation for each: they both use stat = “bin” (in current ggplot2 versions this stat has been split into stat_bin for continuous data and stat_count for discrete data) by default.

# 離散情況
ggplot(diamonds, aes(x = cut)) + geom_bar()
# 等價於ggplot(diamonds, aes(x = cut)) + geom_bar(stat = 'count')

在這裏插入圖片描述

# 等價寫法
ggplot(diamonds, aes(x = cut, y = 1)) + 
geom_bar(stat = 'identity')

在這裏插入圖片描述

# 連續情況
ggplot(diamonds, aes(x = carat)) + geom_bar()
# 等價於ggplot(diamonds, aes(x = cut)) + geom_bar(stat = 'count')
# ggplot(diamonds, aes(x = cut)) + geom_bar(stat = 'bin')會得到不一樣的結果

在這裏插入圖片描述

正負分開圖

head(climate)
A data.frame: 6 × 6
SourceYearAnomaly1yAnomaly5yAnomaly10yUnc10y
<chr><dbl><dbl><dbl><dbl><dbl>
Berkeley1800NANA-0.4350.505
Berkeley1801NANA-0.4530.493
Berkeley1802NANA-0.4600.486
Berkeley1803NANA-0.4930.489
Berkeley1804NANA-0.5360.483
Berkeley1805NANA-0.5410.475
csub <- subset(climate, Source == 'Berkeley' & Year > 1900)
csub$pos  <- csub$Anomaly10y >= 0
head(csub)
A data.frame: 6 × 7
SourceYearAnomaly1yAnomaly5yAnomaly10yUnc10ypos
<chr><dbl><dbl><dbl><dbl><dbl><lgl>
102Berkeley1901NANA-0.1620.109FALSE
103Berkeley1902NANA-0.1770.108FALSE
104Berkeley1903NANA-0.1990.104FALSE
105Berkeley1904NANA-0.2230.105FALSE
106Berkeley1905NANA-0.2410.107FALSE
107Berkeley1906NANA-0.2940.106FALSE
ggplot(csub, aes(x = Year, y = Anomaly10y, fill = pos)) + 
# 設定邊框顏色和寬度
geom_bar(stat = 'identity', color = 'black', size = 0.25) + 
# 手動移除pos圖例
scale_fill_manual(values = c('#8DD35F','#FFE680'), guide = FALSE)

在這裏插入圖片描述

比例堆積圖

  • 查看所有調色板
RColorBrewer::display.brewer.all() 

在這裏插入圖片描述

  • 以date爲切割變量對數據進行transform操作
library(plyr)
ce <- ddply(cabbage_exp, "Date", transform, person_weight = Weight / sum(Weight) * 100)
ggplot(ce, aes(x = Date, y = person_weight, fill = Cultivar)) + 
geom_bar(stat = 'identity', colour = 'black') + 
# 換個調色板
scale_fill_brewer(palette = "Set3")

在這裏插入圖片描述

  • 繪製堆積圖的數字時,要先計算出兩段的高度
cce <- ddply(ce, "Date", transform, text_height = cumsum(person_weight))
# 調整一下堆積圖的順序使之對應
cce$Cultivar <- factor(cce$Cultivar, levels = c("c52", "c39"))
ggplot(cce, aes(x = Date, y = person_weight, fill = Cultivar)) + 
geom_bar(stat = 'identity', colour = 'black') + 
# 換個調色板
scale_fill_brewer(palette = "Set3") + 
geom_text(aes(y = text_height, label = person_weight), vjust = 1.5, color = "black")

在這裏插入圖片描述

  • 進一步美化標註,比如文字放中間,固定位數,加上單位等等
cce <- ddply(ce, "Date", transform, text_height = cumsum(person_weight)-0.5*person_weight)
# 調整一下堆積圖的順序使之對應
cce$Cultivar <- factor(cce$Cultivar, levels = c("c52", "c39"))
ggplot(cce, aes(x = Date, y = person_weight, fill = Cultivar)) + 
geom_bar(stat = 'identity', colour = 'black') + 
# 換個調色板
scale_fill_brewer(palette = "Set3") + 
geom_text(aes(y = text_height, label = paste(round(person_weight, 2), "%")), vjust = 1.5, color = "black", size = 6)

在這裏插入圖片描述

發佈了135 篇原創文章 · 獲贊 56 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章