-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base-and-grid.Rmd
107 lines (79 loc) · 3.39 KB
/
base-and-grid.Rmd
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
title: "Mixing netplot with base graphics"
author: "George G. Vega Yon"
date: 2023-05-22
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Example with baseplots}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Working with grid graphics provides flexibility that is harder to get when using base graphics. Nevertheless, a lot of users may still be more comfortable with base graphics, so this document shows a few examples of how to mix both of them using the [`gridBase`](https://cran.r-project.org/package=gridBase) package.
First, we load igraph to simulate a small network and complement that with uninteresting correlated data:
```{r data-sim, warning=FALSE, message=FALSE, fig.width = 6, fig.height = 5}
library(igraph)
# Simulation parameters
set.seed(12)
n <- 100
# Points
x0 <- rnorm(n)
x1 <- x0*.4 + rnorm(n)
# network
net <- sample_smallworld(1, n, 4, .1)
```
And here, we can create the `netplot` object. We use the [`magrittr`](https://cran.r-project.org/package=magrittr) package to make the workflow nicer:
```{r netplot, fig.width = 6, fig.height = 5}
library(netplot)
library(magrittr)
np <- nplot(net, bg.col = "gray70", vertex.nsides = "square") %>%
set_edge_gpar("line", col = "gray50", alpha = .7) %>%
set_vertex_gpar("frame", fill = "black", col = "black")
```
Imagine that we would like to include a plot of the network in the following plot
```{r baseplot, fig.width = 6, fig.height = 5}
plot(x0, x1)
```
We will use the [`gridBase`](https://cran.r-project.org/package=gridBase) package. `gridBase` allows some interaction between the base and grid graphics systems. In particular, we will use the `baseViewports()` function, which returns a list with `viewports` that can be mapped to match the current base plot
```r
library(gridBase)
library(grid)
# Getting the current viewports
vp <- baseViewports()
```
Once we have the viewports, we can work with these to add the current plot to the base plot. In this case, we will include the network that we just created, `np`, and this will be placed on the top left corner of the plot region and will occupy 1/4 of the area:
```r
# Adding the plot to the current view
pushViewport(vp$plot)
# Sub viewport, top-left corner using 1/4 of the area
pushViewport(viewport(.25, .75, width = .5, height = .5))
# Drawing, we could have used `print(np, newpage=FALSE)`
grid.draw(np)
```
Which yields
```{r alltogether, echo=FALSE, fig.width = 6, fig.height = 5}
library(gridBase)
library(grid)
# Base plot and views
plot(x0, x1)
vp <- baseViewports()
# Adding the plot to the current view
pushViewport(vp$plot)
pushViewport(viewport(.25, .75, width = .5, height = .5))
grid.draw(np)
```
Or suppose that you want to frame it into a rectangle to the top-right corner of the entire device that uses 3/10 x 3/10 of the area, then:
```{r topright, fig.width = 6, fig.height = 5}
plot(x0, x1)
# vp <- baseViewports()
# pushViewport(vp$figure)
pushViewport(viewport(1, 1, just = c("right", "top"), width = .3, height = .3))
grid.draw(np)
```
Notice that we didn't need to extract the viewports of the base plot. A simple viewport did the job since we are using the entire device. Now, imagine that you are feeling playful and would like to draw it a bit tilted, say -20 degrees:
```{r tilted, fig.width = 6, fig.height = 5}
plot(x0, x1)
pushViewport(
viewport(x = .8, y = .8, width = .3, height = .3, angle = -20)
)
grid.draw(np)
```