基本簡(jiǎn)介
分組點(diǎn)圖屁擅,與分組柱狀圖類似,當(dāng)需要在同一個(gè)軸上顯示各個(gè)分類下不同的分組時(shí),需要用到分組柱狀圖氏身。論文中常見的分組箱型圖和分組條形圖(柱狀圖)可以直觀的比較方法的效果,以一個(gè)圖顯示多個(gè)方法在多個(gè)數(shù)據(jù)集上的結(jié)果惑畴。但也有文獻(xiàn)常常使用分組點(diǎn)圖來展示結(jié)果蛋欣。本文使用ggplot2包繪制分組有誤差棒的點(diǎn)圖,這種圖中間的點(diǎn)代表均值如贷,誤差線表示上限和下限(也可以是標(biāo)準(zhǔn)差或者標(biāo)準(zhǔn)誤)陷虎。
基本用法
ggplot2包:geom_point()函數(shù)結(jié)合geom_errorbar()函數(shù)
###geom_point()
geom_point(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
###geom_errorbar()
geom_errorbar(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
###具體幫助可以在Rstudio中加載ggplot2包后使用?geom_point()和?geom_errorbar()分別查看官方幫助文件
示例代碼
#清空環(huán)境
rm(list = ls())
#加載導(dǎo)入數(shù)據(jù)的readxl包和作圖的ggplot2包
library(readxl)
library(ggplot2)
#喜歡新羅馬字體的可以使用windowsFonts()函數(shù)查看電腦字體,進(jìn)行選擇Times New Roman
windowsFonts()
#讀入示例數(shù)據(jù)
df<-read_xlsx("D:/Users/ASUS/Desktop/test.xlsx", sheet = "test")
#查看數(shù)據(jù)
df
這里的數(shù)據(jù)是示例數(shù)據(jù)杠袱,不是真實(shí)的數(shù)據(jù)尚猿,實(shí)際作圖過程中可以替換成自己的數(shù)據(jù)。Treatment為處理(包括A楣富、B凿掂、C、D四個(gè)處理)纹蝴,Yield為產(chǎn)量數(shù)據(jù)(由于本數(shù)據(jù)為示例數(shù)據(jù)庄萎,因此這里不添加單位),Low和Up分別為上限和下限(也可以為標(biāo)準(zhǔn)誤或者標(biāo)準(zhǔn)差塘安,根據(jù)自己情況選擇)糠涛,Site為不同的的研究地點(diǎn)(包括LL和OO兩個(gè)地點(diǎn)),Type為不同的作物類型(Corn和Wheat)
##作圖
ggplot(df, aes(x = Treatment, y=Yield, color = Type))+
geom_errorbar(aes(ymin=Low, ymax=Up), width=.3,position = position_dodge(0.8))+
geom_point(shape = 19, size = 3,position = position_dodge(0.8))+
theme_bw()+
theme(legend.text=element_text(size=14))+
theme(title=element_text(size=14))+
theme(axis.text.x = element_text(size = 14, color = "black"))+
theme(axis.text.y = element_text(size = 14, color = "black"))+
facet_grid(Site~.)+ #這里根據(jù)Site進(jìn)行分面
ylab("Yield")+
guides(color=guide_legend(title = "Type"))+
theme(text=element_text(size=14, family="serif"))#這里設(shè)置字體類型(我們選擇Times New Roman)
若不設(shè)置字體的話兼犯,則默認(rèn)為Arial格式的字體
參考文獻(xiàn)
[1] https://r-graph-gallery.com/ggplot2-package.html
[2] https://cran.r-project.org/web/packages/ggplot2/index.html
[3] https://www.tutorialspoint.com/ggplot2/ggplot2_quick_guide.htm
[4] https://ggplot2.tidyverse.org/