R Under development (unstable) (2025-07-22 r88445 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(grid) > library(gridSVG) Attaching package: 'gridSVG' The following objects are masked from 'package:grid': linearGradient, pattern, radialGradient The following object is masked from 'package:grDevices': dev.off > > pdf(file = NULL) > > # Create the definition of a clipping path, in this case > # it will just be a circle. > cp <- clipPath(circleGrob(r = 0.3)) > > # Register the clipping path so that we can refer to it and apply it. > registerClipPath("circleClip", cp) > > # Creating a simple plot that will be clipped to the circle > sp <- gTree(name = "simplePlot", + children = gList(xaxisGrob(), yaxisGrob(), + rectGrob(gp = gpar(fill = "grey"))), + vp = plotViewport()) > grid.draw(sp) > > # Now lets clip it > grid.clipPath("simplePlot", label = "circleClip") > # Alternatively we could also do this which avoids the need to call > # 'registerClipPath' explicitly > # grid.clipPath("simplePlot", cp) > > # All that remains is a grey circle > grid.export("clippath-simpleplot.svg") > dev.off() null device 1 > > > # Now lets recreate the previous example using 'pushClipPath' > pdf(file = NULL) > # Clear previous clipping path reference > gridSVG.newpage() > > pushViewport(plotViewport()) > > # Create the definition of a clipping path, in this case > # it will just be a circle. > cp <- clipPath(circleGrob(r = 0.3)) > > # Create a new clipping context for this viewport > pushClipPath(cp) > > # Creating a simple plot that will be clipped to our current clipping context > sp <- gTree(name = "simplePlot", + children = gList(xaxisGrob(), yaxisGrob(), + rectGrob(gp = gpar(fill = "grey")))) > grid.draw(sp) > > # End the clipping context > popClipPath() > popViewport() > > # Again we are just left with a grey circle > grid.export("pushclippath-simpleplot.svg") > dev.off() null device 1 > > > proc.time() user system elapsed 0.82 0.07 0.90