-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2026-05-06_ways-to-inset-images.qmd
More file actions
135 lines (105 loc) · 3.17 KB
/
Copy path2026-05-06_ways-to-inset-images.qmd
File metadata and controls
135 lines (105 loc) · 3.17 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---
title: Inset images
subtitle: Three ways to inset images in tidyplots plot
date: 2026-05-06
toc-depth: 4
toc-expand: true
lang: en
---
## 1. Generate a plot first
```{r}
tidyplots |> library()
tibble |> library()
# Generate a data frame
df <- tibble::tibble(
x = seq(1, 10),
y = seq(1, 10)
)
# Plot
p <- df |>
tidyplot(x = x, y = y, paper = "#dddddd") |>
add_title(title = "p") |>
add_data_points() |>
save_plot("images/2026-05-06_p.png", view_plot = FALSE)
```
<center>
<img src="images/2026-05-06_p.png" width="80%" style="border: 1px solid #000000">
</center>
## 2. Read the rabbit image
```{r}
magick |> library()
# The figure is from "https://bioart.niaid.nih.gov/bioart/668"
img <- "images/Rabbit0001.png" |>
magick::image_read()
img |> plot()
```
## 3. Inset rabbit image in tidyplots pot
### 3.1 Method 1: via `adjust_theme_details()` function [@tenu]
```{r}
grid |> library()
ggplot2 |> library()
img_m1 <- img |>
grid::rasterGrob(
x = 0.2, y = 0.8, # geometric of img_m1
width = unit(0.3, "snpc"), # snpc: square normalized parent coordinates
height = unit(0.3, "snpc")
) |>
grid::pattern()
p1 <- p |>
tidyplots::adjust_theme_details(
panel.background = element_rect(fill = img_m1)
) |>
tidyplots::add_title(title = "p1: via adjust_theme_details()") |>
tidyplots::save_plot("images/2026-05-06_via-adjust_theme.png",
view_plot = FALSE)
```
<center>
<img src="images/2026-05-06_via-adjust_theme.png" width="80%" style="border: 1px solid #000000">
</center>
### 3.2 Method 2: via `add()` function
```{r}
img_m2 <- img |>
grid::rasterGrob()
p2 <- p |>
tidyplots::add(annotation_custom(img_m2, xmin = 1, xmax = 4, ymin = 7, ymax = 10)) |>
tidyplots::add_title(title = "p2: via add()") |>
tidyplots::save_plot("images/2026-05-06_via-add.png",
view_plot = FALSE)
```
<center>
<img src="images/2026-05-06_via-add.png" width="80%" style="border: 1px solid #000000">
</center>
### 3.3 Method 3: via patchwork package
```{r}
patchwork |> library()
p3 <- p |>
add_title(title = "p3: via patchwork package")
p3 <- p3 +
patchwork::inset_element(img_m2, left = 0.01, bottom = 0.7, right = 0.3, top = 1)
p3 |>
save_plot("images/2026-05-06_via-patchwork.png",
view_plot = FALSE)
```
<center>
<img src="images/2026-05-06_via-patchwork.png" width="80%" style="border: 1px solid #000000">
</center>
```{r}
#| eval: false
#| echo: false
"images/2026-05-06_via-patchwork.png" |>
magick::image_read() |>
magick::image_border(color = "#000000", geometry = "1x1") |>
magick::image_write("images/2026-05-06_via-patchwork_border.png")
```
## 4. Conclusion
```{r}
patchwork::wrap_plots(p, p1, p2, p3, ncol = 2) |>
tidyplots::save_plot("images/2026-05-06_three-ways.png",
view_plot = FALSE, width = 140, height = 140)
```
<center>
<img src="images/2026-05-06_three-ways.png" width="80%" style="border: 1px solid #000000">
</center>
So, inset via `add()` function or `adjust_theme_details()` function is better than via patchwork package (if no further feature adjustments which I do not know).
[给我买杯茶🍵](给我买杯茶.qmd)
## References