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)記符號,原理一樣眼耀,這里不再重復(fù)英支。
# 添加顯著性標(biāo)記跟上次講的添加數(shù)據(jù)標(biāo)簽是一樣的,這里我們假設(shè)1是對照
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è)置字體。
繪制兩條形圖中間帶有星號的統(tǒng)計圖
#創(chuàng)建一個簡單的數(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)題,最后會提一下鬼廓。(有一個問題就是ggtitle()添加的標(biāo)題總是左對齊)
p + ggtitle("利用ggtitle()添加圖標(biāo)題")
# 利用xlab()\ylab()添加/修改坐標(biāo)軸標(biāo)題
p + ggtitle("利用ggtitle()添加圖標(biāo)題") +
xlab("不同處理") +
ylab("response") #標(biāo)題的參數(shù)修改在theme里肿仑,theme是一個很大的函數(shù),幾乎可以定義一切碎税,下次有時間會講解
最后再講解一下如何將多副圖至于一個頁面 利用包gridExtra中
grid.arrange()
函數(shù)實現(xiàn)
# 將四幅圖放置于一個頁面中
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) #沒有安裝此包先用install.packages('gridExtra')安裝
grid.arrange(p, p1, p2, p3)
上次有人問坐標(biāo)軸旋轉(zhuǎn)的實現(xiàn)尤慰,坐標(biāo)軸旋轉(zhuǎn)有時是很有用的,下面是我看過的一個例子雷蹂,用來介紹一下伟端。
#先加載他的數(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)于世界上各個港口的數(shù)據(jù)匯總
library(dplyr) #用于數(shù)據(jù)操作,與ggplot2一樣是R語言必學(xué)包#現(xiàn)在繪制條形圖(%>%上次說過是管道操作萎河,用于連接各個代碼荔泳,十分有用)
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)簽,二是不用歪著脖子看达舒。
本來打算講講圖例的但是發(fā)現(xiàn)內(nèi)容太多了值朋,就不講了,下次吧巩搏!