【发布时间】:2016-05-22 14:24:06
【问题描述】:
我有一个包含一列连续变量的数据框。我想将这些数据合并到另一列中,以便生成更清晰的图。我是这样做的:
#Add new column to data frame
mydf2["conDistanceBins"] <- NA
#Bin data from conDistance column of df into 5 bins in new column
mydf2$conDistanceBins <- as.numeric(cut2(mydf2$conDistance, g=5))
完成后,我开始尝试进行绘图。现在,当我使用带有以下代码的 ggplot2 生成单个图时,我的图会正确显示并按我希望的那样由 bin 着色:
p9 <- ggplot(mydf2, aes(x = x, y = y))
p9 + geom_point(aes(color=factor(mydf2$conDistanceBins)))
x 和 y 也是 mydf2 数据框中的列。
当我尝试像这样生成分面网格时会出现我的问题:
p7 <- ggplot(mydf2, aes(x, y)) + geom_point(aes(color=factor(mydf2$conDistanceBins)))
p7 + facet_grid(Chromosome~., margins = TRUE)
染色体是我数据框中的另一列。但是,当我尝试运行此代码时,出现以下错误:
Error: Aesthetics must be either length 1 or the same as the data (12390): colour, x, y
我不明白的是,为什么在一个实例中我的代码正在工作,而在另一个实例中却没有,而本质上是第二部分代码不仅采用第一个,而且创建了一个由 Chromosome 列分解的构面网格我的数据框?
编辑:这是我的数据框的一部分。
x y z Gene Chromosome Pos.start boot_avg boot_low
1 -0.2201704 2.2914659 -1.0503592 AGAP000002 X 582 46 5
2 -1.6164962 -0.4252216 4.1920188 AGAP000007 X 83817 25 0
3 0.1585863 -2.1869117 0.5772591 AGAP000010 X 120773 79 2
4 -1.5126431 -0.2293787 2.9891040 AGAP000011 X 127704 54 10
5 -1.5382538 -0.1100106 -0.1838767 AGAP000012 X 146181 84 64
boot_avglow branch_avg branch_low branch_avglow conDistance invDistance
1 9 0.01891250 0.001469 0.001865 4.472136 3.464102
2 0 0.01518050 0.000000 0.000000 6.403124 7.416198
3 39 0.02026960 0.001955 0.003372 3.741657 5.099020
4 10 0.01040867 0.003530 0.003735 6.244998 7.280110
5 67 0.01626420 0.000257 0.001936 4.123106 3.000000
Acceptable Bootstrap Cluster conDistanceBins invDistanceBins
1 Below threshold 1 3 1
2 Below threshold 2 5 5
3 Above threshold 3 2 2
4 Above threshold 2 5 5
5 Above threshold 4 2 1
【问题讨论】:
-
您能否发布一些重现您的问题的示例数据?另外,不确定为什么要将
cut2输出转换为factor到numeric,然后在ggplot代码中返回factor。 -
如果你想只通过染色体分面,你应该使用
facet_grid(~Chromosome, ....)。 -
也是一件小事,你不需要
mydf2$conDistanceBins,你可以将它称为conDistanceBins,数据框在你的ggplot()中指定。 -
嗨,感谢您的反馈,对 R 来说还是很新,所以犯了很多基本错误。我添加了数据框的前 5 行。所以我在上面的例子中想要的是 x 对 y 的散点图,由染色体刻面,并由 conDistanceBins 着色。
-
别担心!将来,
dput(head(mydf2))会更方便我们读取数据。