Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look of arrows in ggplot2 geom_segment()

Tags:

r

ggplot2

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)

enter image description here

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:

enter image description here

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

like image 376
Rasmus Larsen Avatar asked Apr 06 '15 09:04

Rasmus Larsen


People also ask

What does geom_segment do in R?

geom_segment() draws a straight line between points (x, y) and (xend, yend).

How do you make an arrow in R?

To create an arrow R, we can use plot function and arrows function. We just need to understand all the coordinate values that should be passed inside the arrows function. For example, if we have two vectors that contains values from 1 to 10 then the arrow can be created by using arrows function as arrows(1,1,10,10).


1 Answers

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) 

enter image description here

like image 127
user20650 Avatar answered Sep 28 '22 04:09

user20650