r-将数据帧的所有列放入一个直方图中



我有一个类似的数据表

ind1 ind2 ind3 indn
0.1 0.5 1.0 0.0
0.9 0.6 1.0 0.1
0.9 0.7 0.9 0.3

每一列都是一个个体,每一行都是该行基因组坐标下的个体基因型概率。

我想制作一个单一的堆叠直方图,其中x轴是从0到1的GP,y轴是该范围内的站点数量(堆栈根据其来源的个体进行着色(。我以前做过这样的事情:

plN <- ggplot(ndf, aes(x=GP, fill=indiv, color=indiv)) + geom_histogram(binwidth = 0.01)
plot(plN)

在这种情况下,只有两列。

ind1 0.1
ind1 0.9
ind1 0.9
ind2 0.5
ind2 0.6
ind2 0.7...

正如@aosmith所评论的,通常(整洁(的方法是获取长格式的数据进行打印。您可以使用pivot_longertidyr获取长格式的数据。它将获得两列格式的数据,其中name列是列名,value列是该列中的值。

library(ggplot2)
ggplot(tidyr::pivot_longer(ndf, cols = everything()), 
aes(x=value, fill=name, color=name)) + geom_histogram(binwidth = 0.01)

最新更新