【问题标题】:R ggplot2: Error when plotting facet grid that doesn't occur for single plotR ggplot2:绘制单张图不会出现的分面网格时出错
【发布时间】: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 输出转换为factornumeric,然后在ggplot 代码中返回factor
  • 如果你想只通过染色体分面,你应该使用facet_grid(~Chromosome, ....)
  • 也是一件小事,你不需要mydf2$conDistanceBins,你可以将它称为conDistanceBins,数据框在你的ggplot()中指定。
  • 嗨,感谢您的反馈,对 R 来说还是很新,所以犯了很多基本错误。我添加了数据框的前 5 行。所以我在上面的例子中想要的是 x 对 y 的散点图,由染色体刻面,并由 conDistanceBins 着色。
  • 别担心!将来,dput(head(mydf2)) 会更方便我们读取数据。

标签: r ggplot2 facet


【解决方案1】:

您的问题似乎来自mydf2$conDistanceBins。如果您只是将 mydf2$ConDistanceBins 更改为 conDistanceBins 我不会再收到错误消息。请参阅下面的代码和输出:

p7 <- ggplot(mydf2, aes(x, y)) + geom_point(aes(color=factor(conDistanceBins)))
p7 + facet_grid(Chromosome~., margins = TRUE)

数据: 我只使用了您的相关数据:

mydf2<- structure(list(x = c(-0.2201704, -1.6164962, 0.1585863, -1.5126431, 
    -1.5382538), y = c(2.2914659, -0.4252216, -2.1869117, -0.2293787, 
    -0.1100106), z = c(-1.0503592, 4.1920188, 0.5772591, 2.989104, 
    -0.1838767), Gene = structure(1:5, .Label = c("AGAP000002", "AGAP000007", 
    "AGAP000010", "AGAP000011", "AGAP000012"), class = "factor"), 
        Chromosome = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "X", class = "factor"), 
        Pos.start = c(582L, 83817L, 120773L, 127704L, 146181L), boot_avg = c(46L, 
        25L, 79L, 54L, 84L), boot_low = c(5L, 0L, 2L, 10L, 64L), 
        boot_avglow = c(9L, 0L, 39L, 10L, 67L), branch_avg = c(0.0189125, 
        0.0151805, 0.0202696, 0.01040867, 0.0162642), branch_low = c(0.001469, 
        0, 0.001955, 0.00353, 0.000257), branch_avglow = c(0.001865, 
        0, 0.003372, 0.003735, 0.001936), conDistance = c(4.472136, 
        6.403124, 3.741657, 6.244998, 4.123106), invDistance = c(3.464102, 
        7.416198, 5.09902, 7.28011, 3), conDistanceBins = c(3, 5, 
        1, 4, 2)), row.names = c("1", "2", "3", "4", "5"), .Names = c("x", 
    "y", "z", "Gene", "Chromosome", "Pos.start", "boot_avg", "boot_low", 
    "boot_avglow", "branch_avg", "branch_low", "branch_avglow", "conDistance", 
    "invDistance", "conDistanceBins"), class = "data.frame")

【讨论】:

  • 谢谢,这行得通!你知道为什么这种变化会产生影响吗?
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
相关资源
最近更新 更多