File: ngonGrob.Rmd

package info (click to toggle)
r-cran-gridextra 2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,812 kB
  • sloc: makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: "Regular polygons and ellipses in grid graphics"
author: "Baptiste Auguie"
date: '`r Sys.Date()`'
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{ngonGrob: regular polygons and ellipses in grid graphics}
output:
  knitr:::html_vignette:
    toc: yes
---

```{r setup, echo=FALSE, results='hide'}
library(knitr)
opts_chunk$set(message=FALSE, fig.width=4, fig.height=3)
```

The `gridExtra` package provides a basic implementation of regular polygons, `ngonGrob()/grid.ngon`, and a convenience function to draw ellipses, `ellipseGrob()/grid.ellipse()`. We illustrate below the basic usage of these vectorised functions.

## Basic usage

```{r basic}
library(gridExtra)
library(grid)
library(grid)
N <- 5
xy <- polygon_regular(N)*2

# draw multiple polygons
g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
              unit(xy[,2],"cm") + unit(0.5,"npc"),
              n=seq_len(N)+2, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g)
```

## Rotated and stretched polygons

```{r rotated}
g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
              unit(xy[,2],"cm") + unit(0.5,"npc"),
              n=seq_len(N)+2, ar=seq_len(N),
              phase=0, angle=pi/(seq_len(N)+2),
              size=1:N+5, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g2)
```

## Ellipses

```{r ellipse}
g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
                  unit(xy[,2],"cm") + unit(0.5,"npc"),
                  angle=-2*seq(0,N-1)*pi/N+pi/2,
                  size=5, ar=3, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g3)
```