[R] ggplot with arrows
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Tue Aug 19 15:19:43 CEST 2025
>>>>> Paul Murrell via R-help
>>>>> on Mon, 4 Aug 2025 09:00:44 +1200 writes:
> 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
I'm coming a bit late, but ..
Indeed, the above not only helps but is "wonderful" -- truly convincing !
-- thanks to Paul M., the creator of {grid}
(and hence *the* enabler of ggplot2 !)
Martin
> On 2/08/2025 3:24 am, Rui Barradas wrote:
>> On 8/1/2025 4:15 PM, Rui Barradas wrote:
>>> On 8/1/2025 3:43 PM, Ebert,Timothy Aaron wrote:
>>>> I would save the graph as a PowerPoint object and then edit it in
>>>> PowerPoint.
>>>> library(ggplot2)
>>>> library(grid)
>>>> library(officer)
>>>> library(rvg)
>>>> x <- seq(-5, 5, length.out = 100)
>>>> y <- x^2
>>>> data <- data.frame(x, y)
>>>> plot <- ggplot(data, aes(x, y)) +
>>>> geom_path(color = "blue", linewidth = 1.25) +
>>>> geom_segment(
>>>> aes(x = x[1], y = y[1], xend = x[100], yend = y[100]),
>>>> arrow = arrow(angle = 20, type = "closed", ends = "both", length
>>>> = unit(0.2, "inches")),
>>>> color = "red"
>>>> ) +
>>>> theme_linedraw()
>>>> doc <- read_pptx()
>>>> doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
>>>> doc <- ph_with(doc, dml(ggobj = plot), location = ph_location_fullsize())
>>>> print(doc, target = "quadratic_with_arrows.pptx")
>>>>
>>>> If I remember I think you have to ungroup it in PowerPoint and then
>>>> all elements become editable. The general approach can be done with
>>>> other file formats/programs, not just PowerPoint.
>>>> Tim
>>>>
>>>> -----Original Message-----
>>>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Thomas Subia
>>>> via R-help
>>>> Sent: Friday, August 1, 2025 10:31 AM
>>>> To: r-help using r-project.org
>>>> Subject: [R] ggplot with arrows
>>>>
>>>> [External Email]
>>>>
>>>> Consider:
>>>>
>>>> x <- seq(-5,5,length.out = 100)
>>>> y <- x^2
>>>> data <- data.frame(x,y)
>>>> library(ggplot2)
>>>> ggplot(data,aes(x,y))+
>>>> stat_function(
>>>> fun = function(x) x^2,
>>>> color = "blue", linewidth = 1.25
>>>> ) +
>>>> theme_linedraw()
>>>> I'd like to add an arrow to the ends of curve to illustrate the curve
>>>> continues indefinitely in that direction, ChatGPT suggests using
>>>> geom_segment or geom_link but there has an easier way to do this.
>>>>
>>>> Any suggestions would be appreciated.
......
......
More information about the R-help
mailing list