Skip to content
Prev 398045 / 398502 Next

ggplot with arrows

Hi

At the risk of being labelled a "purist", you do not have to leave R to 
gain access to the underlying components and modify them.

The following code combines Rui's tidy up of the ggplot() call with an 
example of directly modifying the underlying 'grid' grob to add arrows 
to the ends.  There are more arguments in arrow() that you can explore 
if you want to.


library(ggplot2)
library(grid)

ggplot() +
   stat_function(
     fun = function(x) x^2,
     color = "blue", linewidth = 1.25,
     xlim = c(-5, 5)
   ) +
   theme_linedraw()

grid.force()
## Check the name of the line grob
## grid.ls()
grid.edit("GRID.polyline", grep=TRUE,
           arrow=arrow(ends="both"))


Hope that helps.

Paul
On 2/08/2025 3:24 am, Rui Barradas wrote: