我正在尝试用ggplot2中的箭头绘制一个类似于此的图,这是使用基础R图形制作的.(颜色不重要)

使用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) )
Run Code Online (Sandbox Code Playgroud)
我明白了:

问题是箭头看起来不对(因为它们看起来不像第一个情节).使用geom_segment()并不重要.
这个问题可能会给出答案,但我希望不那么讨厌: 在R中为网格箭头指定gpar设置
use*_*650 17
更新:ggplot2 v2.1.0.9001
如果绘图位于当前窗口中,则可以直接使用编辑箭头的形状
grid.force()
# change shape of arrows
grid.gedit("segments", gp=gpar(linejoin ='mitre'))
# change the shape in legend also
grid.gedit("layout", gp=gpar(linejoin ='mitre'))
Run Code Online (Sandbox Code Playgroud)
如果绘图位于当前窗口中,则可以直接使用编辑箭头的形状
grid.gedit("segments", gp=gpar(linejoin ='mitre'))
Run Code Online (Sandbox Code Playgroud)
ggplot现在似乎已经将图例键更改为箭头形状,所以如果你想改变它们的形状,你可以在整个情节中做到这一点.
grid.gedit("gTableParent", gp=gpar(linejoin ='mitre'))
Run Code Online (Sandbox Code Playgroud)
原始答案
不少hacky,但也许更容易?您可以编辑返回的grobs ggplotGrob.
如果p是你的情节:
g <- ggplotGrob(p)
idx <- grep("panel", g$layout$name)
nms <- sapply(g$grobs[[idx]]$children[[3]]$children , '[[', "name")
for(i in nms) {
g$grobs[[idx]]$children[[3]] <-
editGrob(g$grobs[[idx]]$children[[3]], nms[i],
gp=gpar(linejoin ='mitre'), grep=TRUE)
}
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

挑战似乎是,如果size在geom_segment块中调用,则网格包中的箭头构造函数会搞乱.
所以
p <- ggplot(df3) + coord_flip()
p1 <- p + geom_bar(aes(x=group,y=max(c(value1,value2))*1.1),width=0.2, stat="identity",position="identity",alpha=0.2)
df1<-filter(df3,time=="1999")
p1 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2),color="blue",size=8,arrow=arrow(angle=20,type="closed",ends="last",length=unit(1,"cm")))
Run Code Online (Sandbox Code Playgroud)
你表现得看起来很荒谬.我尝试了将片段分成一个胖片段和一个瘦片段(两个层)上的箭头的解决方法,如下所示:
p2<-p1 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2), color="blue",arrow=arrow(angle=20,type="closed",ends="last",length=unit(1,"cm")))
p2 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2), color="blue",size=8)
Run Code Online (Sandbox Code Playgroud)
但现在脂肪段末端没有减弱,因此遮挡了箭头.
arrow似乎需要修复参数.