我有這張表,我想繪制花瓣和葉子的開始和結束坐標(一種顏色用于花瓣,另一種用于葉子)。
| 細繩 | ID | 開始 | 結尾 |
|---|---|---|---|
| 花瓣 | ID | 50 | 100 |
| 樹葉 | ID | 101 | 151 |
| 花瓣 | ID | 152 | 202 |
| 樹葉 | ID | 203 | 253 |
| 花瓣 | ID | 254 | 305 |
| 樹葉 | ID | 306 | 356 |
我不想用一個點來表示它,而是用一條從起始坐標開始到結束坐標結束的線來表示花瓣和用不同顏色表示的葉子。
有什么辦法嗎?
uj5u.com熱心網友回復:
我想你可能有這樣的事情:
library(ggplot2)
ggplot(df, aes(y = string, x = start, colour = string))
geom_segment(aes(yend = string, xend = end), size = 3)
scale_color_manual(values = c("yellowgreen", "violet"))
theme_bw()
coord_fixed(20)
labs(x = "Position")

如果您希望線條處于相同的高度,您可以這樣做:
ggplot(df, aes(y = 1, x = start, colour = string))
geom_segment(aes(yend = 1, xend = end), size = 3)
scale_color_manual(values = c("yellowgreen", "violet"))
theme_bw()
coord_fixed(500)
labs(x = "Position")
theme(axis.title.y = element_blank(),
axis.ticks.length.y = unit(0, "mm"),
axis.text.y = element_blank())

資料
df <- structure(list(string = c("petals", "leaves", "petals", "leaves",
"petals", "leaves"), id = c("id", "id", "id", "id", "id", "id"
), start = c(50L, 101L, 152L, 203L, 254L, 306L), end = c(100L,
151L, 202L, 253L, 305L, 356L)), class = "data.frame", row.names = c(NA,
-6L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/441292.html
上一篇:在熊貓資料框中將列值乘以列標題
下一篇:如何更改重采樣列的名稱?
