使用ggplot2从每行数据正确生成直方图

Use ggplot2 to generate histogram from each row of data properly

我有一组数据如下:

pos A   C   G   T
0   0.291398    0.190061    0.315722    0.202818
1   0.315597    0.227511    0.175448    0.281445
2   0.252149    0.194597    0.222815    0.330438

然后我导入了 table:

library(ggplot2)
d = read.table(tablename, sep = '\t', header = T)
d = d[2:5]
data.frame(t(d))

我重新格式化了 table 如下:

        X1       X2       X3
A 0.291398 0.315597 0.252149
C 0.190061 0.227511 0.194597
G 0.315722 0.175448 0.222815
T 0.202818 0.281445 0.330438

然而,当我试图绘制它时:

qplot(X1, data = d, geom = 'histogram')

它给出了下图:

而我想要的应该是这样的:(我用的是libreoffice,所以颜色和宽度等参数都无所谓)

我可以知道如何更正我的代码来制作这个形状吗? 任何帮助表示赞赏。抱歉,我对 R 和 ggplot2 真的很陌生。

您并没有告诉绘图您想要什么作为 Y 值。 X1选择的是你得到的值,不是基数,所有的都存在一次,所以你得到的都是1。

您希望将 X1 作为 Y,将碱基作为 X。

修复你的剧情,来自d:

d$base<-rownames(d)
ggplot(d,aes(x=base,y=X1))+geom_bar(stat="identity")

或使用 qplot 命名法:

d$base<-rownames(d)
qplot(data = d, x = base, y = X1, geom = 'histogram', stat = "identity")

编辑:这是我为所有行绘制它的方式:

library(reshape2)
d1 <- melt(d, id = "pos")
ggplot(d1, aes(x = variable, y = value, fill = factor(pos))) +
       geom_bar(stat = "identity", position = "dodge")