ggplot2 直方图:如何使用 ggplot2 在直方图条上添加文本注释
ggplot2 histogram: how do I add textual annotation onto histogram bars using ggplot2
我正在使用具有以下 header 名称的数据框:
> [1] "Filename" "Strain" "DNA_Source" "Locus_Tag" "Product" "Transl_Tbl" "Note" "Seq_AA" "Protein_ID"
使用以下代码,我得到一个图表,显示在特定细菌菌株中发现了多少基因:
png(filename=paste('images/Pangenome_Histogram.png', sep=''), width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(h <- ggplot(myDF, aes(x=Strain, stat='bin', fill=factor(Filename), label=myDF$Filename)) + geom_bar() +
labs(title='Gene Count by Strain Pangenome', x='Campylobacter Strains', y='Gene Count\n') +
guides(title.theme = element_text(size=15, angle = 90)) + theme(legend.text=element_text(size=15), text = element_text(size=18)) +
theme(axis.text.x=element_text(angle=45, size=16, hjust=1), axis.text.y=element_text(size=16), legend.position='none', plot.title = element_text(size=22)) )
可能有点难看,但例如,一些菌株的条形是 multi-colored——表明该菌株的一些基因来自细菌染色体以外的来源(或来自多个染色体)如果细菌有多个染色体)。我想根据基因的来源("DNA_Source")在适当的位置标记条形图。
png(filename=paste('images/Pangenome_Histogram.png', sep=''), width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(h <- ggplot(myDF, aes(x=Strain, stat='bin', fill=factor(Filename), label=myDF$Filename)) + geom_bar() +
labs(title='Gene Count by Strain Pangenome', x='Campylobacter Strains', y='Gene Count\n') +
guides(title.theme = element_text(size=15, angle = 90)) + theme(legend.text=element_text(size=15), text = element_text(size=18)) +
geom_text(aes(label=DNA_Source, y='identity'), color='black', vjust=-5, size=4) +
theme(axis.text.x=element_text(angle=45, size=16, hjust=1), axis.text.y=element_text(size=16), legend.position='none', plot.title = element_text(size=22)) )
这让我很接近,但是它从 y-axis 中删除了计数(并在左下角添加了 "identity" 一词)并且它在彼此的顶部标记了贡献所以除非是同一个词,否则无法读取它们。
我希望 y-axis 像第一张图片一样标记,第二张图片带有标签 -- 但是 我希望这些标签出现在他们的直方图的相应颜色部分(在视觉上类似于此处:Showing data values on stacked bar chart in ggplot2),但我想使用 ggplot2 包 来完成它。
我希望这是清楚的。感谢您的帮助 - 提前致谢。
这是一些数据 (tail(dput(myDF[c(2, 3, 5)])))...
Strain DNA_Source Product
12299 Campy3194c Plasmid Type VI secretion protein, VC_A0111
12300 Campy3194c Plasmid Type VI secretion protein
12301 Campy3194c Plasmid Tgh104
12302 Campy3194c Plasmid protein ImpC
12303 Campy3194c Plasmid Type VI secretion protein
12304 Campy3194c Chromosome Tgh079
假设您有一个如下所示的数据集:
library(data.table)
library(ggplot2)
set.seed(123)
dna_src <- c("Chromosome", "Plasmid")
myDF <- data.table(Strain = c(rep("Campy3149c", 100),
rep("Campy31147q", 100)),
DNA_Source = c(sample(dna_src, size = 100, replace = T,
prob = c(0.9, 0.1)),
sample(dna_src, size = 100, replace = T,
prob = c(0.7, 0.3))))
head(myDF)
# Strain DNA_Source
#1: Campy3149c Chromosome
#2: Campy3149c Chromosome
#3: Campy3149c Chromosome
#4: Campy3149c Chromosome
#5: Campy3149c Plasmid
#6: Campy3149c Chromosome
您可以使用 data.table
将数据折叠为更短的 data.table
,其中包含我们需要的大部分信息,唯一添加的是我们计算的标签的 y 值如下:
dt <- myDF[, .(countStrain = .N), by = c("Strain", "DNA_Source")][order(Strain, DNA_Source)]
# add the y-values for the plot
dt[, yval := cumsum(countStrain) - 0.5 * countStrain, by = Strain]
最后,我们绘制值
ggplot(dt, aes(x = Strain, y = countStrain, fill = DNA_Source)) +
geom_bar(stat = "identity") +
geom_text(data = dt, aes(x = Strain, y = yval, label = DNA_Source))
结果如下图:
我正在使用具有以下 header 名称的数据框:
> [1] "Filename" "Strain" "DNA_Source" "Locus_Tag" "Product" "Transl_Tbl" "Note" "Seq_AA" "Protein_ID"
使用以下代码,我得到一个图表,显示在特定细菌菌株中发现了多少基因:
png(filename=paste('images/Pangenome_Histogram.png', sep=''), width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(h <- ggplot(myDF, aes(x=Strain, stat='bin', fill=factor(Filename), label=myDF$Filename)) + geom_bar() +
labs(title='Gene Count by Strain Pangenome', x='Campylobacter Strains', y='Gene Count\n') +
guides(title.theme = element_text(size=15, angle = 90)) + theme(legend.text=element_text(size=15), text = element_text(size=18)) +
theme(axis.text.x=element_text(angle=45, size=16, hjust=1), axis.text.y=element_text(size=16), legend.position='none', plot.title = element_text(size=22)) )
可能有点难看,但例如,一些菌株的条形是 multi-colored——表明该菌株的一些基因来自细菌染色体以外的来源(或来自多个染色体)如果细菌有多个染色体)。我想根据基因的来源("DNA_Source")在适当的位置标记条形图。
png(filename=paste('images/Pangenome_Histogram.png', sep=''), width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(h <- ggplot(myDF, aes(x=Strain, stat='bin', fill=factor(Filename), label=myDF$Filename)) + geom_bar() +
labs(title='Gene Count by Strain Pangenome', x='Campylobacter Strains', y='Gene Count\n') +
guides(title.theme = element_text(size=15, angle = 90)) + theme(legend.text=element_text(size=15), text = element_text(size=18)) +
geom_text(aes(label=DNA_Source, y='identity'), color='black', vjust=-5, size=4) +
theme(axis.text.x=element_text(angle=45, size=16, hjust=1), axis.text.y=element_text(size=16), legend.position='none', plot.title = element_text(size=22)) )
这让我很接近,但是它从 y-axis 中删除了计数(并在左下角添加了 "identity" 一词)并且它在彼此的顶部标记了贡献所以除非是同一个词,否则无法读取它们。
我希望 y-axis 像第一张图片一样标记,第二张图片带有标签 -- 但是 我希望这些标签出现在他们的直方图的相应颜色部分(在视觉上类似于此处:Showing data values on stacked bar chart in ggplot2),但我想使用 ggplot2 包 来完成它。
我希望这是清楚的。感谢您的帮助 - 提前致谢。
这是一些数据 (tail(dput(myDF[c(2, 3, 5)])))...
Strain DNA_Source Product
12299 Campy3194c Plasmid Type VI secretion protein, VC_A0111
12300 Campy3194c Plasmid Type VI secretion protein
12301 Campy3194c Plasmid Tgh104
12302 Campy3194c Plasmid protein ImpC
12303 Campy3194c Plasmid Type VI secretion protein
12304 Campy3194c Chromosome Tgh079
假设您有一个如下所示的数据集:
library(data.table)
library(ggplot2)
set.seed(123)
dna_src <- c("Chromosome", "Plasmid")
myDF <- data.table(Strain = c(rep("Campy3149c", 100),
rep("Campy31147q", 100)),
DNA_Source = c(sample(dna_src, size = 100, replace = T,
prob = c(0.9, 0.1)),
sample(dna_src, size = 100, replace = T,
prob = c(0.7, 0.3))))
head(myDF)
# Strain DNA_Source
#1: Campy3149c Chromosome
#2: Campy3149c Chromosome
#3: Campy3149c Chromosome
#4: Campy3149c Chromosome
#5: Campy3149c Plasmid
#6: Campy3149c Chromosome
您可以使用 data.table
将数据折叠为更短的 data.table
,其中包含我们需要的大部分信息,唯一添加的是我们计算的标签的 y 值如下:
dt <- myDF[, .(countStrain = .N), by = c("Strain", "DNA_Source")][order(Strain, DNA_Source)]
# add the y-values for the plot
dt[, yval := cumsum(countStrain) - 0.5 * countStrain, by = Strain]
最后,我们绘制值
ggplot(dt, aes(x = Strain, y = countStrain, fill = DNA_Source)) +
geom_bar(stat = "identity") +
geom_text(data = dt, aes(x = Strain, y = yval, label = DNA_Source))
结果如下图: