本筆記內(nèi)容:
最近工作中遇到的分析需求:按照要求的分組畫boxplot和PcoA的散點(diǎn)圖。對(duì)畫各種圖的實(shí)現(xiàn)方法,一些具體問題的解決方法等。
- Long data和wide data之間的轉(zhuǎn)換, 及其作用
- 作圖細(xì)節(jié):box的顏色,散點(diǎn)分布,調(diào)整label的角度, 修改label: theme(), labs()等
- 調(diào)整橫坐標(biāo)labels的順序:設(shè)置factor中l(wèi)evel的順序
- facet_grid(): 將一個(gè)Plot按照分組繪制多個(gè)Plot,并在theme()中設(shè)置它的屬性
- ggplot2的一些通用規(guī)律
- PCoA:ade4和ggplot2
- legend()用法
- theme()快捷設(shè)置
- 零碎的東西
- 在boxplot之間連線
- geom_bar()
- geom_, geom_, geom_
- scale_, scale_, scale_
- error bar
long data和wide data之間的轉(zhuǎn)換, 及其作用
關(guān)于Long data和wide data之間的轉(zhuǎn)換及其意義,我寫過在python中的用法,見python學(xué)習(xí):pandas學(xué)習(xí)筆記(三)中Pandas.melt()的用法。在這里是用R實(shí)現(xiàn)。一般ggplot2需要使用Long data. 比方說如下所示的數(shù)據(jù)cond1 , cond2 和control 你都想用ggplot2畫boxplot出來(lái),那必須轉(zhuǎn)化為L(zhǎng)ong data, 把每個(gè)樣本的測(cè)量值分一列,測(cè)得是cond1還是cond2一列。
了解更多參考這個(gè)鏈接
t <- read.table(header=TRUE, text='
subject sex control cond1 cond2
1 M 7.9 12.3 10.7
2 F 6.3 10.6 11.1
3 F 9.5 13.1 13.8
4 M 11.5 13.4 12.9
')
t_long <- melt(t, id.vars = c('subject', 'sex')) # id.vars為不合并的列,即保留的列
t_long
subject sex variable value
1 1 M control 7.9
2 2 F control 6.3
3 3 F control 9.5
4 4 M control 11.5
5 1 M cond1 12.3
6 2 F cond1 10.6
7 3 F cond1 13.1
8 4 M cond1 13.4
9 1 M cond2 10.7
10 2 F cond2 11.1
11 3 F cond2 13.8
12 4 M cond2 12.9
作圖細(xì)節(jié):box的顏色,散點(diǎn)分布等
以以上數(shù)據(jù)為例,按照variable的分組畫三個(gè)boxplot:
ggplot(t_long, aes(x = variable, y = value)) +
geom_boxplot()
ggplot(t_long, aes(x = variable, y = value, fill = variable)) +
geom_boxplot() + geom_point(position = position_jitterdodge()) +
# 在box中加上點(diǎn),讓點(diǎn)隨機(jī)排列,不要忘記用加號(hào)銜接
scale_fill_brewer(palette = "Set3") + # 使用brewer.pal中的調(diào)色盤
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
# theme()用來(lái)設(shè)置labels, 以及l(fā)abels與各軸之間的角度
labs(x = "condition", y = "condition_values")
# 重命名labels
ggplot(t_long, aes(x = variable, y = value, fill = sex)) +
geom_boxplot(position = position_dodge(0.8)) +
# 因?yàn)榘汛骲oxplot拆分成兩個(gè),設(shè)置這兩個(gè)小boxplot之間的距離
geom_point(position = position_jitterdodge()) +
scale_fill_brewer(palette = "Set3") +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
labs(x = "condition", y = "condition_values")
# ggplot(aes(fill = ))變化了,將每個(gè)variable拆分成sex的兩個(gè)小組畫圖
可以參考這個(gè)鏈接,一個(gè)基礎(chǔ)又詳細(xì)的教程。
調(diào)整橫坐標(biāo)labels的順序:設(shè)置factor中l(wèi)evel的順序
c <- t_long$value
t_long$variable <- factor(t_long$variable ,
levels =unique(t_long$variable[order(-c)]))
# 讓整個(gè)boxplot大致降序排列,需要將包含box類別的變量設(shè)置為factor,在level中設(shè)置為按照值來(lái)降序排列
# ....真是麻煩啊...如果有什么別的好辦法還請(qǐng)告訴我
將boxplot降序或者升序排列,在fill = 變量非常多,把整個(gè)圖拉的很長(zhǎng)的情況下很有用,可以很明確的看出數(shù)據(jù)的規(guī)律。最好能在input dataframe的時(shí)候就把數(shù)據(jù)整理成一定順序。
還有一個(gè)辦法:
ggplot(t_long, aes(x = reorder(variable, value, FUN = median)), y = ...)
# 在ggplot的aes()里指定按照中位數(shù)大小排列
facet_grid():將一個(gè)Plot按照分組繪制多個(gè)Plot
使用一個(gè)R自帶的數(shù)據(jù)集為例:data(ToothGrowth) ,這個(gè)數(shù)據(jù)集有兩個(gè)分組,一個(gè)是supp, 一個(gè)是dose, 記得需要先把dose轉(zhuǎn)化為factor再行后續(xù)操作。
t <- data(ToothGrowth)
t$dose <- as.factor(t$dose)
p2 <- ggplot(t, aes(x = supp, y = len, fill = supp)) +
geom_boxplot(position = position_dodge(0.8)) +
geom_point(position = position_jitterdodge()) +
scale_fill_brewer(palette = "Set3") +
facet_grid(dose ~ .,scales = "free") + # 按照dose的分組將plot分成3個(gè)不同的dose子plot; scales的意思是按照各分組數(shù)據(jù)的極限值設(shè)置各子plot的scale, 不統(tǒng)一。
theme(axis.title = element_text(size = 15),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size =12),
strip.text.y = element_text(size = 12), # 調(diào)整不同子plot的標(biāo)簽字體大小
panel.spacing = unit(1, "lines")) + # 調(diào)整不同子plot之間距離大小
labs(x = "", y = "")
更多參考以下鏈接:
http://www./english/wiki/ggplot2-customize-how-to-personalize-easily-ggplot2-graphs-in-r-statistical-software
http://www./english/wiki/ggplot2-facet-split-a-plot-into-a-matrix-of-panels
ggplot2的一些通用規(guī)律
ggplot(dataframe, aes(x = X, y = Y, fill = group, color = group)) +
# 設(shè)置了fill則將"可以fill的形狀"填充起來(lái),比方說橢圓,比方說boxplot, barplot等。不設(shè)置fill什么顏色則ggplot2默認(rèn)填充。用scale_fill_**設(shè)置填充顏色
# color用于設(shè)置點(diǎn)或者線的顏色,比方說boxplot的外框描線...
+ geom_point(aes(color = group), alpha = 0.8, size = 4)
# 在aes()中設(shè)置點(diǎn)的顏色,但是點(diǎn)的透明度,大小等屬性注意在aes()之外設(shè)置
# 在aes()之外,position = position_jitterdodge()將點(diǎn)按照分組分開
+ geom_boxplot()
+ scale_color_manual(values = c(xxx,xxx,xxx...))
# scale_color_**用于自定義color的顏色,即點(diǎn),線的顏色
+ scale_fill_manual(values = c(xxx, xxx, xxx...))
# scale_fill_**用于自定義fill的顏色,即填充顏色
PCoA:ade4和ggplot2
ade4包:以距離矩陣為input, 用cmdscale()獲取坐標(biāo)軸位置,用s.class畫圖并按照分組聚類??墒菦]有坐標(biāo)軸信息,也不知道這兩維分別可以有多少variance explained
unifrac <- read.table(...)
meta <- read.csv(...)
mds <- cmdscale(unifrac,k = 2, eig = TRUE)
mds_m <- mds$points
group <- as.factor(meta$grouping) # 把分組信息轉(zhuǎn)化為Factor
plot_color <- brewer.pal(5,"Set2")[group] # 便于給每個(gè)樣本分組并上色
s.class(mds_m, col=unique(plot_color), cpoint = 1, fac = group, cstar = 1, cellipse = 1)
# 按照分組形成聚類橢圓(wheel)
# 用fake data畫的
ggplot2包:需要把metadata和cmdscale()得到的mds坐標(biāo)合并為一個(gè)dataframe作為input, 并使用上面ade4包得到的mds$eig計(jì)算variance explained
可以參考一篇宏基因組公眾號(hào)的文章: 擴(kuò)增子統(tǒng)計(jì)繪圖2散點(diǎn)圖:Beta多樣性
eig <- mds$eig
mds_df <- data.frame(mds_m)
mds_meta <- cbind(mds_df, meta)
# X1, X2為各點(diǎn)坐標(biāo)信息的col_name,grouping為分組信息的col_name
# 都在mds_meta中
ggplot(mds_meta, aes(x = X1, y = X2)) +
geom_point(aes(color = factor(grouping)), size = 3) +
scale_color_discrete(name = "grouping") +
stat_ellipse(aes(x = X1, y = X2, color = grouping), type = "norm") +
labs(x = paste("PCoA 1 (", format(100*eig[1]/sum(eig), digits = 4), "%)",sep = ""),
y = paste("PCoA 2 (", format(100*eig[2]/sum(eig), digits = 4), "%)",sep = "")
)
# 手動(dòng)添加variance explained的label
legend()用法
group <- as.factor(data$group)
color <- brewer.pal(length(levels(group)), "Set1")
heatmap.2(x...)
par(lend = 1)
legend(0.88, 0.998, # 也可以直接指定:“bottomright”, “bottom”, “bottomleft”, “l(fā)eft”, “topleft”, “top”, “topright”, “right”, “center”
legend = levels(group), # 指定legend中寫什么字
col = color, #指定顏色
lty = 1, # 顯示為直線型色塊
lwd = 10, # 顯示色塊的寬度
text.col = "black",# legend字體顏色
pch = c(.., .., .....) # 如果你的legend lty沒有設(shè)置,想設(shè)置成不同形狀不同顏色的legend,則使用pch指定色塊的形狀,具體代碼對(duì)應(yīng)的形狀見下圖
cex = 0.8) # 設(shè)置字體大小
參考連接:https://www./packages/graphics/versions/3.5.0/topics/legend
theme()快捷設(shè)置
default為+ theme_gray()
常用 theme_classic() , 詳見以下鏈接
http://ggplot2./reference/ggtheme.html
theme_classic(size = n) 設(shè)置默認(rèn)classic的主題中字體大小
如何去掉legend title/legend
ggplot() + theme(legend.title=element_blank())
theme(legend.position = "none") 則去除legend
給boxplot加上統(tǒng)計(jì)檢驗(yàn)結(jié)果及P值
https://www./add-p-values-and-significance-levels-to-ggplots/
ggarrange() 把圖組合在一起,公用legend
ggarrange(plotlist = plot_list, ncol = 4, common.legend = TRUE)
如果有一個(gè)list of plots, 可以用ggarrange(plotlist...)
合并在一起,且可以公用legend, 設(shè)置legend的位置。
boxplot(fill = )和scale_fill_manual()
往往要在boxplot(fill=)中設(shè)置了顏色才可以顯示!那要scale_fill_manul多么雞肋!要你何用!
在boxplot之間連線
詳見這個(gè)鏈接
ggplot(..) +
geom_line(aes(group = interaction(index, variable)) +...
# index將要連接的點(diǎn)聯(lián)系在一起
geom_bar()
geom_bar(position = 'fill'/'dodge')
舉個(gè)栗子:
library(socviz) # 用這里的gss_sm數(shù)據(jù)集
ggplot(data= gss_sm, aes(x=bigregion,fill=religion)) +
geom_bar(position='fill')
ggplot(data= gss_sm, aes(x=bigregion,fill=religion)) +
geom_bar(position='dodge')
分別得到:
另外:
-
geom_bar() 和geom_histogram() 的區(qū)別:
histogram用于查看單個(gè)變量的distribution,當(dāng)然也可以把幾個(gè)變量的distribution放在一張圖里,比較他們的分布。
bar圖用于查看多個(gè)變量的計(jì)數(shù)/占比/數(shù)值大小。
-
geom_bar(stat = "identity") 和 geom_col() 是一樣的,都表示把對(duì)應(yīng)的y值直接plot出來(lái),不用做任何stat summary
geom_,geom_,geom_...
geom_dotplot()
geom_smooth() 平滑曲線,或者線性的直線
geom_pointrange(ymin=.. ,ymax=...) 給點(diǎn)加上range,比方說Sd, 你必須先準(zhǔn)備好sd的數(shù)據(jù),不能指望ggplot2給你做好。這里可以注意與dplyr的結(jié)合使用,提高效率,減少中間變量。
facet_wrap(~ variable, scales = "free_y", ncol = 1) 注意scales的用法
scale_, scale_,scale_
ggplot(aes(...)) 的完整是ggplot(mapping = aes(x = .., y =.., color = ..., fill = ... ...)) , 每個(gè)mapping中的東西,都具有一個(gè)scale. x和y是連續(xù)型變量,其scale交給scale_x_log10() , scale_x_continuous() 來(lái)調(diào)整。像mapping中的color, fill ,shape等, 需要scale_color_discrete()等來(lái)調(diào)整,比如以下這個(gè)例子:
library(socviz)
data(organdata) # 示例數(shù)據(jù)
ggplot(organdata, aes(x=roads, y= donors, color= world)) +
geom_point()
ggplot(organdata, aes(x=roads, y= donors, color= world)) +
geom_point() +
scale_color_discrete(labels = c("Corporatist", "Liberal", "Social Democratic", "Unclassified")) +
labs(x = "Road Deaths", y = "Donor Procurement", color = "Welfare State")
# scale_color_manual(name = "XXX", values = XXXcolors)
error bar
在ggplot2中實(shí)現(xiàn)有很多種方法,包括:
geom_pointrange()
geom_linerange()
geom_crossbar()
geom_errorbar()
|