我正在尝试使用坐标绘制一些多边形。这些坐标表示沿着序列(x轴)的多个要素的相对位置,并针对多个条件(y轴)重复进行。当x轴值相同时,可以正确绘制这些多边形,但是在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
答案 0 :(得分:0)
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)
似乎不适用于多个组。