1.频数直方图
数据准备
> head(pacBioData)
V1 V2
1 Ghir_A01G000010 1
2 Ghir_A01G000030 1
3 Ghir_A01G000040 3
4 Ghir_A01G000070 3
5 Ghir_A01G000080 1
6 Ghir_A01G000100 2
如果你同时含有两个样本的频数信息,需要针对不同的样本画不同的的柱子;只需要在数据框中添加一个字段即可。例如我的数据中含有PacBio测序数据和参考基因组数据两个文件
pacBioData<- read.table("PacBio文件")
referenceData <- read.table("reference文件")
##添加分类信息
pacBioData$type <- "PacBio"
referenceData$type <- "reference"
## 合并两个数据框
mergeData <- rbind(pacBioData, referenceData)
绘制直方图
fill = type
针对type字段使用不同颜色进行填充
stat = "count"
统计x轴中每个值出现的次数,用作柱子的高度
library(ggplot2)
ggplot(data = mergeData, aes(x = V2, fill = type)) +
geom_bar(stat = "count")
调整
position = "dodge"
将柱子调整为不堆积状态
这里由于x轴的坐标轴范围比较大,柱子缩放了看不清
ggplot(data = mergeData, aes(x = V2, fill = type)) +
geom_bar(stat = "count", position = "dodge", width = 0.5)
2.频率直方图
数据准备
使用R中的table
函数和prop.table
函数计算频率
PacBioFrequent <- as.data.frame(prop.table(table(pacBioData$V2)))[1:10, ]
referenceFrequent <- as.data.frame(prop.table(table(referenceData$V2)))[1:10, ]
## 样本分类
PacBioFrequent$type <- "PacBio"
referenceFrequent$type <- "reference"
## 合并数据
mergeData <- rbind(PacBioFrequent, referenceFrequent)
处理后的数据
Var1 Freq type
1 1 0.28121310 PacBio
2 2 0.21962588 PacBio
3 3 0.15747934 PacBio
4 4 0.10686098 PacBio
5 5 0.07342614 PacBio
绘制图形
参数和之前的都是一样的
ggplot(data = mergeData, aes(x =Var1, y=Freq,fill = type)) +
geom_bar(stat = "identity", position = "dodge", width = 0.5)
3.美化图片
不涉及数据层的美化
p=ggplot(data = mergeData, aes(x =Var1, y=Freq,fill = type)) +
geom_bar(stat = "identity", position = "dodge", width = 0.5)
p+theme_bw() +
theme(
panel.grid = element_blank(), #图片网格线
panel.background = element_blank(), #图片背景色为空
axis.line = element_line(size = 0.5, color = "black"),#坐标轴线条
axis.text.x = element_text(size = "15"), #x坐标轴刻度文字
axis.text.y = element_text(size = "10"), #y坐标轴刻度文字
axis.title.y = element_text(size = "15"), #x坐标轴label文字
legend.title = element_blank(), #图例标题
legend.position = c(0.8, 0.8), #图例位置
)
自定义填充色
p+ scale_fill_manual(values = c(
"#f6e58d", "#ffbe76", "#686de0", "#4834d4"
))
调整柱子离坐标轴位置
这里我将柱子与x轴进行贴近,其他的可以类似
p+ scale_y_continuous(expand = c(0, 0), limits = c(0, 1))
添加自定义文字
通过使用geom_text
函数,并且选择不继承原有的图片数据
inherit.aes
是否继承图形数据,如果是使用自定义数据,这里一定要FALSE
#将要展示的注释文字
labelData <- data.frame(text1 = "3.39", text2 = "/", text3 = "2.01")
p+geom_text(
data = labelData,
mapping = aes(x = 8, y = 0.5, label = text1),
inherit.aes = FALSE,
show.legend = NA,
color = "#ffbe76"
) +
geom_text(
data = labelData,
mapping = aes(x = 8.5, y = 0.5, label = text2),
inherit.aes = FALSE,
show.legend = NA,
color = "black"
) +
geom_text(
data = labelData,
mapping = aes(x = 9, y = 0.5, label = text3),
inherit.aes = FALSE,
show.legend = NA,
color = "#4834d4"
)
参考