我試圖用ggplot2中的箭頭繪製一個情節,看起來像這樣,這是使用基本的R圖片製作的。 (顏色並不重要)ggplot2中的箭頭geom_segment()
使用GGPLOT2:
library(ggplot2)
library(scales)
library(grid)
df3 <- structure(list(value1 = c(51L, 57L, 59L, 57L, 56L, 56L, 60L,
66L, 61L, 61L), value2 = c(56L, 60L, 66L, 61L, 61L, 59L, 61L,
66L, 63L, 63L), group = c("A", "B", "C", "D", "E", "A", "B",
"C", "D", "E"), time = c("1999", "1999", "1999", "1999", "1999",
"2004", "2004", "2004", "2004", "2004"), y_position = c(1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L)), .Names = c("value1", "value2",
"group", "time", "y_position"), row.names = c(NA, -10L), class = "data.frame")
ggplot(df3, aes(x = value1, y = y_position, group = time, color = time)) +
geom_segment(x = min(df3$value1, df3$value2), xend = max(df3$value1, df3$value2),
aes(yend = y_position), color = "lightgrey", size = 19) +
scale_y_continuous( labels = df3$group, breaks = df3$y_position) +
theme_classic() + theme(axis.line = element_blank(), axis.title = element_blank() ) +
geom_segment(aes(yend = y_position, xend = value2, color = time, group = time), size = 19, alpha = 0.9,
arrow = arrow(length = unit(40, "points"),type = "closed", angle = 40) )
我得到這個:
的問題是,箭頭看上去是錯誤的(因爲它們看起來不像第一個情節)。使用geom_segment()並不重要。
這個問題可以給出答案,但我希望少哈克的東西: Specifying gpar settings for grid arrows in R
是否有可能使第二個箭頭從第一個箭頭結束時自動開始,還是必須以某種方式手動執行? – 2015-04-20 07:13:09
我不認爲這可以輕鬆完成Rasmus。如果您注意到箭頭的結束超出了實際的xend位置,但超出範圍(以原生比例)取決於圖的寬度。所以很難爲第二個箭頭指定一個開始位置(如果有人能夠顯示的話,會很棒)。也許,一個快速的解決辦法是扭轉水平,所以第二個箭頭在第一個/下面。 – user20650 2015-04-22 08:59:15
@ user20650不幸的是,似乎你的答案不再適用於'ggplot 2.2.0'。如果您有任何時間和可能性更新您的答案,它將不勝感激。提前致謝。 – Henrik 2016-11-25 15:57:58