ggplot2中的箭头geom_segment() [英] Look of arrows in ggplot2 geom_segment()

查看:481
本文介绍了ggplot2中的箭头geom_segment()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2中的箭头绘制一个情节,看起来像这样,这是使用基本R图片制作的。 (颜色不重要)





使用ggplot2:

  library(ggplot2)
库(比例)
库(网格)


df3< - 结构(列表(值1 = c(51L,57L,59L,57L,56L,56L,60L ,
66L,61L,61L),值2 = c(56L,60L,66L,61L,61L,59L,61L,
66L,63L,63L),group = c(A B,C,D,E,A,B,
,C,D,E, ,1999,1999,1999,
2004,2004,2004,2004,2004),y_position = c(1L,2L,$ b $ (例如b 3L,4L,5L,1L,2L,3L,4L,5L)),.Names = c(value1,value2,
group,time,y_position .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 =箭头(length = unit(40,points),type =closed,angle = 40))

我得到这个:


问题在于箭头看起来不对(因为它们不是'看起来像第一个情节)。使用geom_segment()并不重要。



这个问题可能会给出答案,但我希望避免这样的做法:
为R中的网格箭头指定gpar设置

解决方案

update:ggplot2 v2.1.0.9001



如果情节出现在当前窗口中,您可以编辑直接用箭头指向

  grid.force()
#箭头形状改变
grid.gedit( segments,gp = gpar(linejoin ='miter'))
#改变图例中的形状也是
grid.gedit(layout,gp = gpar(linejoin ='miter'))






如果情节出现在当前窗口中,您可以编辑直接用箭头的形状

  grid.gedit(segments,gp = gpar(linejoin ='miter')) 

ggplot现在似乎改变了图例键为箭头形状,所以如果你想改变它们的形状,你可以在整个绘图中用

  grid.gedit(gTableParent,gp = gpar(linejoin ='miter'))






原始答案

不少于hacky,但也许更容易?您可以编辑由 ggplotGrob 返回的grobs。



如果 p

  g < -  ggplotGrob(p)

idx< - grep(panel,g $ layout $ name)

nms< - sapply(g $ grobs [[idx]] $ children [[3]] $ children,'[['名称)

(i in nms){
g $ grobs [[idx]] $ children [[3]]< -
editGrob(g $ grobs [ idx]] $ children [[3]],nms [i],
gp = gpar(linejoin ='miter'),grep = TRUE)
}

grid。 newpage()
grid.draw(g)


I'm trying to make a plot with arrows in ggplot2 looking something like this, which was made using base R grapics. (colors are not important)

Using 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)  )

I get this:

The problem is that the arrows look wrong (in that they don't look like the first plot). Using geom_segment() is not important.

This question may give the answer but I was hoping for something less hacky: Specifying gpar settings for grid arrows in R

解决方案

update: ggplot2 v2.1.0.9001

If the plot is in your current window you can edit the shape of the arrow directly with

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'))


If the plot is in your current window you can edit the shape of the arrow directly with

grid.gedit("segments", gp=gpar(linejoin ='mitre'))

ggplot now seems to have changed the legend key to an arrow shape, so if you want to change the shape of these as well, you can do this across the full plot with

grid.gedit("gTableParent", gp=gpar(linejoin ='mitre'))


original answer

Not less hacky, but perhaps easier?? You can edit the grobs returned by ggplotGrob.

If p is your plot:

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)

这篇关于ggplot2中的箭头geom_segment()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆