【问题标题】:Polygons not being correctly drawn多边形未正确绘制
【发布时间】:2020-02-09 17:57:07
【问题描述】:

我正在尝试使用坐标绘制一些多边形。这些坐标表示沿序列(x 轴)的多个特征的相对位置,在多个条件下重复(y 轴)。当 x 轴值相同时,这些多边形被正确绘制,但当它们不同时,这些多边形被正确绘制 - 我必须在这里遗漏一些关于 ggplot 如何绘制多边形但无法解决的明显问题! (我对使用 ggplot 很陌生)

这给出了所需的输出:

ids <- factor(c("cox1", "atp8"))

values <- data.frame(
  id = ids,
  value = c("cox1", "atp8")
)

positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(1, 10, 10, 1, 11, 20, 20, 11,
        1, 10, 10, 1, 11, 20, 20, 11 ),
  y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 
        5, 5, 6, 6, 6.1, 6.1, 7.1, 7.1)
)

# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by = c("id"))
p.labs <- p + labs(title = "Mito genomes" , x = "Position (bp)", y = "Individual")
p <- ggplot(datapoly, aes(x = x, y = y)) +
  geom_polygon(aes(fill = value, group = id)) + 
  coord_fixed(ratio = 5) + 
  scale_y_discrete(limit = c(1.5, 5.5), labels = c("1234", "14"))

p
p.labs

但是当我更改第二个条件(“14”)的 x 坐标时,多边形变成了连接:

ids <- factor(c("cox1", "atp8"))

values <- data.frame(
  id = ids,
  value = c("cox1", "atp8")
)

positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(1, 10, 10, 1, 11, 20, 20, 11,
        2, 11, 11, 2, 12, 21, 21, 12 ),
  y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 
        5, 5, 6, 6, 6.1, 6.1, 7.1, 7.1)
)

# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by = c("id"))
p.labs <- p + labs(title = "Mito genomes" , x = "Position (bp)", y = "Individual")
p <- ggplot(datapoly, aes(x = x, y = y)) +
  geom_polygon(aes(fill = value, group = id)) + 
  coord_fixed(ratio = 5) + 
  scale_y_discrete(limit = c(1.5, 5.5), labels = c("1234", "14"))

p
p.labs

[

我的想法是,对于第一种情况,多边形也是“连接”的,但是因为它们直接在彼此上方,所以你看不到。如何指定这些多边形需要保持分离?非常感谢!

编辑: 下面的答案建议使用组,这似乎可以解决问题,但是当我在 x 轴上添加更多多边形时,它会再次混淆(见下文)!显然,我从根本上误解了如何指定组/坐标,我一直在绞尽脑汁但无法解决!任何帮助将不胜感激!:

ids <- factor(c("atp6", "atp8", "cob", "cox1", "cox2")) 

values <- data.frame(
  id = ids,
  value = c("atp6", "atp8", "cob", "cox1", "cox2")
)

indiv <- factor(c( "013", "023" ))

individuals <- data.frame(
  id = indiv,
  value = c("013", "023") #, "1008", "101")
)

positions <- data.frame(
  id = rep(ids, each = 4),
x = c(1, 1575, 1575, 1, 1541, 1601, 1601, 1541, 1602, 2288, 2288, 1602, 2290, 2350, 2350, 2290, 2351, 2515, 2515, 2351,
      1, 1557, 1557, 1, 1541, 1603, 1603, 1541, 1605, 2285, 2285, 1605, 2288, 2350, 2350, 2288, 2351, 2509, 2509, 2351),
y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 
      4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5) 
)

# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by = c("id"))
p.labs <- p + labs(title = "Mito genomes" , x = "Position (bp)", y = "Individual")
datapoly$group1 <- rep(1:10, each = 4)
p <- ggplot(datapoly, aes(x = x, y = y)) +
  geom_polygon(aes(fill = value, group = group1)) + 
  coord_fixed(ratio = 500) + 
  scale_y_discrete(limit = c(1.5, 5.5, 9.5, 13.5), labels = c("013", "023", "1008", "101"))

p

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    group 美学决定了哪些坐标被视为同一多边形的一部分。通过给每个矩形一个唯一的组标识符,您可以防止这种合并。

    datapoly$group <- rep(1:4, each = 4)
    p <- ggplot(datapoly, aes(x = x, y = y)) +
      geom_polygon(aes(fill = value, group = group)) + 
      coord_fixed(ratio = 5) + 
      scale_y_discrete(limit = c(1.5, 5.5), labels = c("1234", "14"))
    

    编辑:如果有更多组,这似乎是你想要的情节,不是吗?:

    ids <- factor(c("atp6", "atp8", "cob", "cox1", "cox2"))
    positions <- data.frame(
      id = rep(ids, each = 4),
      x = c(1, 1575, 1575, 1, 1541, 1601, 1601, 1541, 1602, 2288, 2288, 1602, 2290, 2350, 2350, 2290, 2351, 2515, 2515, 2351,
            1, 1557, 1557, 1, 1541, 1603, 1603, 1541, 1605, 2285, 2285, 1605, 2288, 2350, 2350, 2288, 2351, 2509, 2509, 2351),
      y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 
            4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5) 
    )
    
    ggplot(positions, aes(x, y, group = id, fill = id)) +
      geom_polygon() +
      coord_fixed(ratio = 500) +
      scale_y_discrete(limit = c(1.5, 5.5, 9.5, 13.5), labels = c("013", "023", "1008", "101"))
    

    可能是merge 操作对多边形的排序做了一些奇怪的事情,因此rep(1:10, each = 4) 似乎不适用于多个组。

    【讨论】:

    • 谢谢!这适用于这个例子,但是当我扩大规模时,我仍然遇到问题!我有 16x 4 边多边形,所以我有 64 个 x 坐标和 64 个 y 坐标,我设置:datapoly$group
    • 如果数据排序正确,理论上应该可以工作,但是你确定你的坐标是以 4 行为一组排序的吗?
    • 这工作正常:x = c(1, 1575, 1575, 1, 1541, 1601, 1601, 1541, 1602, 2288, 2288, 1602, 2290, 2350, 2350, 2290, 1, 1557, 1557, 1, 1541, 1603, 1603, 1541, 1605, 2285, 2285, 1605, 2288, 2350, 2350, 2288), y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1 , 1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1) $group1
    • 但这不起作用:x = c(1, 1575, 1575, 1, 1541, 1601, 1601, 1541, 1602, 2288, 2288, 1602, 2290, 2350, 2350, 2290 , 2351, 2515, 2515, 2351, 1, 1557, 1557, 1, 1541, 1603, 1603, 1541, 1605, 2285, 2285, 1605, 2288, 2350, 2350, 2288, 2351, 259) y = c(1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 2.1, 2.1, 3.1, 3.1, 1, 1, 2, 2, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5, 5.1, 5.1, 6.1, 6.1, 4, 4, 5, 5) datapoly$group1
    • 所以唯一的区别是我在 ( x = 2351, 2515, 2515, 2351 / y = 1, 1, 2, 2 ) & ( x = 2351, 2509, 2509 上添加了 2x 多边形, 2351 / y = 4, 4, 5, 5 ) 并将代表从 1:8 更改为 1:10..?
    猜你喜欢
    • 2021-12-28
    • 2015-11-24
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多