ggplot2學(xué)習(xí)筆記系列之利用ggplot2繪制誤差棒及顯著性標(biāo)記

YanTao

繪制帶有誤差棒的條形圖

library(ggplot2)
# 創(chuàng)建數(shù)據(jù)集
df <- data.frame(treatment = factor(c(1, 1, 1, 2, 2, 2, 3, 3, 3)), 
                 response = c(2, 5, 4, 6, 9, 7, 3, 5, 8), 
                 group = factor(c(1, 2, 3, 1, 2, 3, 1, 2, 3)), 
                 se = c(0.4, 0.2, 0.4, 0.5, 0.3, 0.2, 0.4, 0.6, 0.7))
head(df) #查看數(shù)據(jù)集

##   treatment response group  se
## 1    1         2       1    0.4
## 2    1         5       2    0.2
## 3    1         4       3    0.4
## 4    2         6       1    0.5
## 5    2         9       2    0.3
## 6    2         7       3    0.2
# 使用geom_errorbar()繪制帶有誤差棒的條形圖
# 這里一定要注意position要與`geom_bar()`保持一致,由于系統(tǒng)默認(rèn)dodge是0.9,
# 因此geom_errorbar()里面position需要設(shè)置0.9,width設(shè)置誤差棒的大小
ggplot(data = df, aes(x = treatment, y = response, fill = group)) + 
geom_bar(stat = "identity", position = "dodge") + 
geom_errorbar(aes(ymax = response + se, ymin = response -  se), 
position = position_dodge(0.9), width = 0.15) + 
scale_fill_brewer(palette = "Set1")

繪制帶有顯著性標(biāo)記的條形圖

label <- c("", "*", "**", "", "**", "*", "", "", "*") #這里隨便設(shè)置的顯著性,還有abcdef等顯著性標(biāo)記符號(hào),原理一樣,這里不再重復(fù)。
# 添加顯著性標(biāo)記跟上次講的添加數(shù)據(jù)標(biāo)簽是一樣的,這里我們假設(shè)1是對(duì)照
ggplot(data = df, aes(x = treatment, y = response, fill = group)) + 
geom_bar(stat = "identity", position = "dodge") + 
geom_errorbar(aes(ymax = response + se, ymin = response -  se), 
position = position_dodge(0.9), width = 0.15) + 
geom_text(aes(y = response +  1.5 * se, label = label, group = group), 
position = position_dodge(0.9), size = 5, fontface = "bold") + 
scale_fill_brewer(palette = "Set1") #這里的label就是剛才設(shè)置的,group是數(shù)據(jù)集中的,fontface設(shè)置字體。

繪制兩條形圖中間帶有星號(hào)的統(tǒng)計(jì)圖

#創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)據(jù)集
Control <- c(2.0,2.5,2.2,2.4,2.1)
Treatment <- c(3.0,3.3,3.1,3.2,3.2)
mean <- c(mean(Control), mean(Treatment))
sd <- c(sd(Control), sd(Treatment))
df1 <- data.frame(V=c("Control", "Treatment"), mean=mean, sd=sd)
df1$V <- factor(df1$V, levels=c("Control", "Treatment"))
#利用geom_segment()繪制圖形
ggplot(data=df1, aes(x=V, y=mean, fill=V))+ 
geom_bar(stat = "identity",position = position_dodge(0.9),color="black")+ 
geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), width=0.05)+ 
geom_segment(aes(x=1, y=2.5, xend=1, yend=3.8))+#繪制control端的豎線 
geom_segment(aes(x=2, y=3.3, xend=2, yend=3.8))+#繪制treatment端豎線 
geom_segment(aes(x=1, y=3.8, xend=1.45, yend=3.8))+ 
geom_segment(aes(x=1.55, y=3.8, xend=2, yend=3.8))+#繪制兩段橫線 
annotate("text", x=1.5, y=3.8, label="〇", size=5)#annotate函數(shù)也可以添加標(biāo)簽

為圖形添加標(biāo)題

圖形標(biāo)題有圖標(biāo)題、坐標(biāo)軸標(biāo)題、圖例標(biāo)題等

p <- ggplot(data = df, aes(x = treatment, y = response, fill = group)) + 
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar(aes(ymax = response + se, ymin = response -  se),
position = position_dodge(0.9), width = 0.15) + 
scale_fill_brewer(palette = "Set1")# 利用ggtitle()添加圖標(biāo)題,還有l(wèi)abs()也可以添加標(biāo)題,最后會(huì)提一下。(有一個(gè)問(wèn)題就是ggtitle()添加的標(biāo)題總是左對(duì)齊)
p + ggtitle("利用ggtitle()添加圖標(biāo)題")
# 利用xlab()\ylab()添加/修改坐標(biāo)軸標(biāo)題
p + ggtitle("利用ggtitle()添加圖標(biāo)題") + 
xlab("不同處理") + 
ylab("response") #標(biāo)題的參數(shù)修改在theme里,theme是一個(gè)很大的函數(shù),幾乎可以定義一切,下次有時(shí)間會(huì)講解


最后再講解一下如何將多副圖至于一個(gè)頁(yè)面 利用包gridExtragrid.arrange()函數(shù)實(shí)現(xiàn)

# 將四幅圖放置于一個(gè)頁(yè)面中
p <- ggplot(data = df, aes(x = treatment, y = response, fill = group)) + 
geom_bar(stat = "identity", position = "dodge") + 
geom_errorbar(aes(ymax = response + se, ymin = response -  se), 
position = position_dodge(0.9), width = 0.15) + 
scale_fill_brewer(palette = "Set1")
p1 <- p + ggtitle("利用ggtitle()添加圖標(biāo)題")
p2 <- p + ggtitle("利用ggtitle()添加圖標(biāo)題") + xlab("不同處理") + ylab("response")
p3 <- ggplot(data = df, aes(x = treatment, y = response, fill = group)) + 
geom_bar(stat = "identity", position = "dodge") + 
geom_errorbar(aes(ymax = response + se, ymin = response -  se), 
position = position_dodge(0.9), width = 0.15) + 
geom_text(aes(y = response +  1.5 * se, label = label, group = group), 
position = position_dodge(0.9), size = 5, fontface = "bold") + 
scale_fill_brewer(palette = "Set1")
library(gridExtra) #沒(méi)有安裝此包先用install.packages('gridExtra')安裝
grid.arrange(p, p1, p2, p3)

上次有人問(wèn)坐標(biāo)軸旋轉(zhuǎn)的實(shí)現(xiàn),坐標(biāo)軸旋轉(zhuǎn)有時(shí)是很有用的,下面是我看過(guò)的一個(gè)例子,用來(lái)介紹一下。

#先加載他的數(shù)據(jù)
url.world_ports <- url("http://sharpsightlabs.com/wp-content/datasets/world_ports.RData")
load(url.world_ports)
knitr::kable(df.world_ports[1:5,])#該數(shù)據(jù)是關(guān)于世界上各個(gè)港口的數(shù)據(jù)匯總
library(dplyr) #用于數(shù)據(jù)操作,與ggplot2一樣是R語(yǔ)言必學(xué)包#現(xiàn)在繪制條形圖(%>%上次說(shuō)過(guò)是管道操作,用于連接各個(gè)代碼,十分有用)
df.world_ports%>%filter(year==2014)%>% #篩選2014年的數(shù)據(jù) 
ggplot(aes(x=reorder(port_label, desc(volume)), y=volume))+ 
geom_bar(stat = "identity", fill="darkred")+ 
labs(title="Busiest container ports in the world")+ 
labs(subtitle = '2014, in order of shipping volume')+ #添加副標(biāo)題 
labs(x = "Port", y = "Shipping\nVolume")+ 
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .4))#調(diào)整x軸標(biāo)簽,angle=90表示標(biāo)簽旋轉(zhuǎn)90度,從圖中可以看出
#現(xiàn)在旋轉(zhuǎn)坐標(biāo)軸,并篩選排名小于25的港口,并且添加數(shù)據(jù)標(biāo)簽
df.world_ports %>% filter(year==2014, rank<=25) %>% #篩選2014年并且rank小于等于25的數(shù)據(jù) 
ggplot(aes(x=reorder(port, volume), y=volume))+ 
geom_bar(stat = "identity", fill="darkred")+ 
labs(title="Busiest container ports in the world")+ 
labs(subtitle = '2014, in order of shipping volume')+  
labs(x = "Port", y = "Shipping\nVolume")+ 
geom_text(aes(label=volume), hjust=1.2, color="white")+ 
coord_flip()#旋轉(zhuǎn)坐標(biāo)軸

兩圖相比,明顯第二幅圖好,一是可以添加數(shù)據(jù)標(biāo)簽,二是不用歪著脖子看。
本來(lái)打算講講圖例的但是發(fā)現(xiàn)內(nèi)容太多了,就不講了,下次吧!

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Matplotlib 入門教程 來(lái)源:Introduction to Matplotlib and basic l...
    布客飛龍閱讀 32,268評(píng)論 5 162
  • 簡(jiǎn)介 文章較長(zhǎng),點(diǎn)擊直達(dá)我的博客,瀏覽效果更好。本文內(nèi)容基本是來(lái)源于STHDA,這是一份十分詳細(xì)的ggplot2使...
    taoyan閱讀 51,718評(píng)論 7 159
  • 上一篇 redis cluster 集群暢談二 , 主要講解 實(shí)驗(yàn)多master寫入、讀寫分離、實(shí)驗(yàn)自動(dòng)故障切換...
    逐暗者閱讀 2,662評(píng)論 0 3
  • 今天從老家回到了昆山,坐了一天的車。明天抽空把春晚看了,對(duì)所有節(jié)目一一進(jìn)行點(diǎn)評(píng),今天看了胡歌的,劉濤的。潘長(zhǎng)江的,...
    云牽閱讀 179評(píng)論 0 0
  • 工作了將近三年的時(shí)候,離得臆想的風(fēng)花雪月越來(lái)越遠(yuǎn),離得自己的認(rèn)知水平越來(lái)越近。 不過(guò)是承認(rèn)自己偽了多年文藝青年。小...
    慢半拍shining閱讀 445評(píng)論 2 0

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