8.3.3 條形圖相關(guān)操作
1懂牧、條形寬度和間距調(diào)整 geom_bar()函數(shù)中width調(diào)整條形寬度,默認值為0.9疯坤,取值范圍0-1;簇狀條形圖默認組內(nèi)條形間距為0深浮,要調(diào)整組內(nèi)條形間距可將width設(shè)定小一些压怠,并令position_dodge()的取值大于width。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) + geom_bar(stat = "identity") # 默認的是0.9飞苇。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) + geom_bar(stat = "identity", width = 0.5) # width調(diào)整為0.5菌瘫。
data1 %>%
ggplot(aes(x = nitrogen, y = v1, fill = nitrogen)) + geom_bar(stat = "identity", width = 1) # width取值最大只能是1洋闽。
data1 %>%
ggplot(aes(x = nitrogen, y = v1, fill = variety)) + geom_bar(stat = "identity", position = "dodge") # 分組柱狀圖,position=position_dodge()默認參數(shù)是0.9突梦。
data1 %>%
ggplot(aes(x = nitrogen, y = v1, fill = variety)) + geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7)) # 同時設(shè)定width和position_dodge()調(diào)整組內(nèi)間距诫舅。
2、x軸分組順序調(diào)整
對于 X軸變量為類別型的數(shù)據(jù)宫患,使用ggplot2包繪圖時刊懈,會默認將X軸類別按照字母順序繪制柱形。一般推薦對數(shù)據(jù)進行降序處理后娃闲,再展示圖表皇帮,這樣渐白,更加方便觀察數(shù)據(jù)規(guī)律襟诸,確定某個類別對應(yīng)的數(shù)值在整個數(shù)據(jù)范圍的位置菇用。scale_x_discrete()函數(shù)中的limits參數(shù)可調(diào)整分類變量順序。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) + geom_bar(stat = "identity") # 基礎(chǔ)圖拴鸵。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + scale_x_discrete(limits = c("N2", "N4", "N1", "N3")) # 調(diào)整分組順序伤靠。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + scale_x_discrete(limits = rev(levels(factor(data1$nitrogen)))) # 反轉(zhuǎn)因子順序妄田。
3斟珊、數(shù)據(jù)標簽添加geom_text()函數(shù)可映射要添加的數(shù)據(jù)標簽。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + geom_text(aes(label = v1)) # 添加數(shù)據(jù)標簽富纸。
為什么會出現(xiàn)重疊囤踩,因為我的數(shù)據(jù)沒有進行統(tǒng)計。不是用均值進行繪圖的晓褪。
data1 %>%
group_by(nitrogen) %>%
summarise(mean_v1 = mean(v1), sd_v1 = sd(v1)) %>%
ggplot(aes(x = nitrogen, y = mean_v1)) +
geom_bar(stat = "identity") + geom_text(aes(label = round(mean_v1, 2)), vjust = -0.5) # 添加數(shù)據(jù)標簽堵漱。
4、x和y軸互換 coord_flip()函數(shù)用于反轉(zhuǎn)坐標軸涣仿。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) + geom_bar(stat = "identity") # 基礎(chǔ)圖勤庐。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + coord_flip() # 翻轉(zhuǎn)坐標軸。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") +
coord_flip() + scale_x_discrete(limits = rev(levels(factor(data1$nitrogen)))) # 翻轉(zhuǎn)坐標軸并翻轉(zhuǎn)因子順序好港。
5愉镰、刻度位置調(diào)整
標度函數(shù)scale_y_continuous()中的breaks用于調(diào)整刻度線位置。ggplot柱形圖縱坐標起始不是從0開始的钧汹,即x軸與圖柱之間有間隙丈探,可在 scale_y_continuous()中調(diào)整expand參數(shù)。 要調(diào)整y軸范圍拔莱,可通過ylim()實現(xiàn)碗降,也可以在scale_y_continuous()中設(shè)定limits參數(shù)隘竭。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) + geom_bar(stat = "identity") # 基礎(chǔ)圖。y軸刻度0到15讼渊,間隔單位5动看。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + scale_y_continuous(breaks = c(0, 5, 8, 10, 12, 15, 18)) # 調(diào)整為自定義刻度。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + scale_y_continuous(expand = c(0, 0)) # 調(diào)整y軸從0開始爪幻,去除與x軸的間隙弧圆。
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + scale_y_continuous(expand = c(0, 0), limits = c(0, 1.8)) # 開始,去除與x軸的間隙笔咽。
## Warning: Removed 44 rows containing missing values (`geom_bar()`).
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") + ylim(0, 2) # 設(shè)置y閾值為0-2搔预。
## Warning: Removed 44 rows containing missing values (`geom_bar()`).
6、添加誤差線geom_errorbar()函數(shù)將變量映射到y(tǒng)min和ymax的值即可叶组。
data1 %>%
group_by(nitrogen) %>%
summarise(mean_v1 = mean(v1), sd_v1 = sd(v1)) %>%
ggplot(aes(x = nitrogen, y = mean_v1)) +
geom_bar(stat = "identity") + geom_errorbar(aes(ymin = mean_v1 - sd_v1, ymax = mean_v1 + sd_v1), width = 0.2)
7拯田、添加顯著性結(jié)果 假設(shè)我已經(jīng)對數(shù)據(jù)data1進行了單因素方差分析,經(jīng)分析四個氮水平的差異顯著性用字母標注是c,ab,b,a甩十。
data1 %>%
group_by(nitrogen) %>%
summarise(mean_v1 = mean(v1), sd_v1 = sd(v1)) %>%
ggplot(aes(x = nitrogen, y = mean_v1)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymin = mean_v1 - sd_v1, ymax = mean_v1 + sd_v1), width = 0.2) + geom_text(aes(x = nitrogen, y = mean_v1 + sd_v1 + 0.2, label = c("c", "ab", "b", "a")), size = 3, fontface = "bold")
ggpubr包添加顯著性標識
library(ggpubr) # 調(diào)用ggpubr包船庇。compare_means(v1 ~ nitrogen, data = data1, method = "t.test") # 先根據(jù)分組進行t檢驗。
## # A tibble: 6 × 8
## .y. group1 group2 p p.adj p.format p.signif method
## <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
## 1 v1 N1 N2 0.000225 0.0011 0.00023 *** T-test
## 2 v1 N1 N3 0.00130 0.0052 0.00130 ** T-test
## 3 v1 N1 N4 0.00000118 0.0000071 1.2e-06 **** T-test
## 4 v1 N2 N3 0.652 0.65 0.65232 ns T-test
## 5 v1 N2 N4 0.0717 0.14 0.07171 ns T-test## 6 v1 N3 N4 0.0354 0.11 0.03545 * T-test
data1 %>%
ggplot(aes(x = nitrogen, y = v1)) +
geom_bar(stat = "identity") +
stat_compare_means(comparisons = list(c("N1", "N2"),
c("N2", "N3"),
c("N3", "N4")),
method = "t.test",
label = "p.format", label.y = c(15, 17, 19)) # 根據(jù)檢驗結(jié)果添加顯著性結(jié)果侣监。
## [1] FALSE
參考資料
ggplot2: 數(shù)據(jù)分析與圖形藝術(shù)鸭轮,西安交通大學出版社,2013.
R語言數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南橄霉,電子工業(yè)出版社窃爷,2019.
R數(shù)據(jù)科學,人民郵電出版社姓蜂,2018.
R數(shù)據(jù)可視化手冊按厘,人民郵電出版社,2014.
- END -
本文使用 文章同步助手 同步