-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypoimg_recolor_svg.Rmd
108 lines (86 loc) · 3.01 KB
/
hypoimg_recolor_svg.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
106
107
108
---
title: "Recolor svg files"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Recolor svg files}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{css, echo=FALSE}
img {
border: 0;
}
```
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dev = "ragg_png"
)
options(warn = - 1) # suppress warnings in grid due to rotated grobs (for successful vignette building)
library("ragg")
library("hypoimg")
```
## Load svg files
The **hypoimg** package provides tools read external svg files into R to use for plot annotation.
(The svg needs to be a cairo svg - to transform your svg into
this format use the **rsvg** package)
```{r, eval = FALSE}
rsvg::rsvg_svg("image.svg","image.c.svg")
```
```{r basicUsage, message = FALSE, warning = FALSE, out.width = "95%", fig.width = 12}
library(tidyverse)
library(grImport2)
library(grid)
library(gridSVG)
svg_file <- system.file("extdata", "logo.c.svg", package = "hypoimg")
svg <- hypo_read_svg(svg_file)
ggplot(tibble(x=rnorm(100),y=rnorm(100)))+
annotation_custom(svg)+
geom_point(aes(x=x,y=y),
color="#AA1F00")
```
## Recolor single layer
Individual layers of the image can be recolored using the `hypo_recolor_svg()` function:
```{r recolorSingle, out.width = "95%", fig.width = 12, message = FALSE, warning = FALSE}
svg_new <- hypo_recolor_svg(svg, layer = 1, color = "#2B5B99")
ggplot()+
annotation_custom(svg_new)
```
This can be used to create a table of color variants:
```{r recolorTable, out.width = "95%", fig.width = 12, fig.height = 6, message = FALSE, warning = FALSE}
n = 9
tab <- hypo_recolor_grob_table(svg,
LETTERS[1:n],
scico::scico(n, palette = 'lapaz'),
angle = rnorm(n)*60,
layer = 1)
ggplot(tibble(x=1:2),aes(x=x,y=x))+
geom_hypo_grob(data=tab,
aes(grob=grob,x=x,y=y,
angle=angle),
width=.6)+
facet_wrap(grp~.,ncol = 3)+
theme(text=element_blank(),
axis.ticks = element_blank())
```
Finally, `hypo_recolor_svg()` can be applied multiple times to recolor several layers:
```{r recolorMultiple, out.width = "95%", fig.width = 12, fig.height = 4, message = FALSE, warning = FALSE}
svg_file_2 <- system.file("extdata", "logo2.c.svg", package = "hypoimg")
svg_2 <- hypo_read_svg(svg_file_2)
clr <- scico::scico(25)
ggplot()+
annotation_custom(svg_2,xmax = .5)+
annotation_custom(svg_2 %>%
hypo_recolor_svg(.,layer = 1,clr[1]) %>%
hypo_recolor_svg(.,layer = 2,clr[8]) %>%
hypo_recolor_svg(.,layer = 3,clr[16]) %>%
hypo_recolor_svg(.,layer = 4,clr[24]),
xmin = .5)
```
---------------------------------
```{r, echo = FALSE, fig.asp = 1, out.height = "150pt", out.width = "150pt", fig.align = "center"}
grImport2::grid.picture(grImport2::readPicture("logo.c.svg"))
```