首页 > 解决方案 > 多边形未正确绘制

问题描述

我正在尝试使用坐标绘制一些多边形。这些坐标表示沿序列(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

[多边形不是分开的[1]

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

编辑:下面的答案建议使用组,这似乎可以解决问题,但是当我在 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

在此处输入图像描述

标签: rggplot2

解决方案


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)对于多个组似乎不正确。


推荐阅读