ggplot :几个直方图合二为一

ggplot : several histogram as one

我想使用 ggplot 绘制几种生物信息学工具的基准测试结果。我希望所有条形图都在同一张图上,而不是每个工具都有一张图。我已经有了 LibreOffice 的输出(见下图),但我想用 ggplot 重新做。

现在我对每个工具都有这种代码(以第一个为例):

data_reduced <- read.table("benchmark_groups_4sps", sep="\t", header=TRUE)

p<-ggplot(data=data_reduced, aes(x=Nb_sps, y=OrthoFinder)) +
    geom_bar(stat="identity", color="black", fill="red") +
    xlab("Number of species per group") + ylab("Number of groups") +
    geom_text(aes(label=OrthoFinder), vjust=1.6, color="black", size=3.5)

但我还没有找到如何将所有图形粘贴在一起,但不知道如何将它们合并为一个。

我的输入数据:

Nb_species  OrthoFinder FastOrtho   POGS (no_para)  POGS (soft_para)    proteinOrtho
4   125 142 152 202 114
5   61  65  42  79  44
6   37  29  15  21  8
7   19  17  4   7   5
8   15  10  1   0   0
9   10  2   0   0   0

谢谢!

也许这可以帮助您找到正确的方向:

# sample data
df = data.frame(Orthofinder=c(1,2,3), FastOrtho=c(2,3,4),   POGs_no_para=c(1,2,2))

library(reshape2)
library(dplyr)

# first let's convert the dataset: Convert to long format and aggregate.
df = melt(df, id.vars=NULL)
df = df %>% group_by(variable,value) %>% count()

# Then, we create a plot.
ggplot(df, aes(factor(value), n, fill = variable)) + 
  geom_bar(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1")

关于格式化绘图的文档已经足够多了,所以我会把它留给你;)希望这对你有帮助!

EDIT: Since the question was changed to work with a different dataset as origin while I was typing my answer, here is the modified code to work with that:

df = data.frame(Nb_species = c(4,5,6,7),  OrthoFinder=c(125,142,100,110), FastOrtho=c(100,120,130,140))

library(reshape2)
library(dplyr)
df = melt(df, id.vars="Nb_species")

ggplot(df, aes(factor(Nb_species), value, fill = variable)) + 
  geom_bar(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1")