本期內(nèi)容為[跟著NC學(xué)作圖]-繪制頻率分布圖(圖中圖)
--

頻率直方圖.png
早期自己做的圖,如下:
image
那么,我們看看這個(gè)圖形是如何繪制的呢?
一、數(shù)據(jù)輸入
options(stringsAsFactors = FALSE)
library(ggplot2)
library(plyr)
library(dplyr)
library(cowplot)
## 導(dǎo)入數(shù)據(jù)
seq_metrics <- read.table("20221130_inputdata.txt", header = T, sep = "\t")
> seq_metrics
sample ID coinf_maj_common coinf_maj_ratio coinf_min_match coinf_min_common pl Year
Pl789-021216596401_S93 21216596401 35 0.897 0 789 21
Pl789-021216614001_S96 21216614001 45 0.978 BA_3 1 789 21
Pl789-021216651701_S57 21216651701 38 0.731 0 789 21
Pl789-021216703901_S75 21216703901 39 0.929 0 789 21
Pl789-021216741701_S74 21216741701 38 0.884 0 789 21
Pl789-021216770101_S61 21216770101 38 0.884 0 789 21
Pl789-021216803301_S81 21216803301 33 0.805 0 789 21
Pl781-021216860001_S11 21216860001 38 0.826 0 781 21
Pl783-021216910001_S214 21216910001 35 0.778 0 783 21
Pl781-021216920801_S15 21216920801 37 0.725 0 781 21
Pl789-021216977901_S86 21216977901 42 0.75 0 789 21
Pl781-021217011901_S6 21217011901 37 0.902 0 781 21
Pl782-021217049001_S187 21217049001 43 0.915 0 782 21
Pl781-021217063201_S2 21217063201 38 0.95 0 781 2
數(shù)據(jù)處理
根據(jù)year和pl進(jìn)行分類,分成第一和第二
那么我們可以根據(jù)自己的需求將其分成自己需要的分類即可
first_replicate = ddply(seq_metrics, .(ID),function(x) x[order(x$Year,x$pl),][1,])
seq_metrics$Replicate = NA
seq_metrics$Replicate[is.element(seq_metrics$sample,first_replicate$sample)] = "First"
seq_metrics$Replicate[!is.element(seq_metrics$sample,first_replicate$sample)] = "Second"
在最后一列輸出了分類信息。這就是我們最后的畫(huà)圖的數(shù)據(jù)類型。我們也可以自己手動(dòng)修改的。
二、繪圖
繪制First類的圖形
## 繪制基礎(chǔ)圖形
ggplot(seq_metrics[seq_metrics$Replicate=="First",], aes(x=coinf_min_common))+
geom_histogram(,binwidth=1)
image
p <-ggplot(seq_metrics[seq_metrics$Replicate=="First",], aes(x=coinf_min_common))+
geom_histogram(,binwidth=1)+
ggtitle("All samples") +
xlab("Number of specific-variants from secondary lineage") +
theme_bw()
image
繪制,縮小部分的圖形
ggplot(seq_metrics[seq_metrics$Replicate=="First"& seq_metrics$coinf_min_common>1,], aes(x=coinf_min_common))+
geom_histogram(,binwidth=1)+
ggtitle("Samples with more than 1 specific-variants\nfrom secondary lineage") +
xlab("Number of specific-variants from secondary lineage") +
theme_bw()
image
三、合并
ggdraw(p +theme_half_open(12)) + #theme_half_open(12) 第一張圖的大小
draw_plot(p_inset, .25, .25, .75, .75) ## (位置:左右,上下;圖形大?。鹤笥遥舷?
image
四、頻率分布圖geom_histogram()函數(shù)的參數(shù)
geom_histogram(
mapping =NULL, #映射
data =NULL, #數(shù)據(jù)集
stat ="bin", #直方圖
position ="stack", #位置
..., #其他geom類函數(shù)的參數(shù)
binwidth =NULL, #直方圖的間距
bins =NULL, #直方個(gè)數(shù),和binwidth有類似的效果
na.rm =FALSE, #邏輯個(gè)數(shù),
orientation =NA, #方向
show.legend =NA, #邏輯參數(shù),是否顯示該圖層的圖例,NA為默認(rèn)
inherit.aes =TRUE #邏輯參數(shù),是否疊加本圖層和默認(rèn)的幾何要素
)
往期文章(總匯)
小杜的生信筆記 ,主要發(fā)表或收錄生物信息學(xué)的教程,以及基于R的分析和可視化(包括數(shù)據(jù)分析,圖形繪制等);分享感興趣的文獻(xiàn)和學(xué)習(xí)資料!