• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    R
  • License
    Other
  • Created almost 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use SVG images as ggplot points

ggsvg - Use SVG as points in ggplot

R-CMD-check

ggsvg is an extension to ggplot to use SVG images for points.

Variables may be aesthetically mapped to features within the SVG using CSS selectors via the css() helper function.

Whatโ€™s in the box

  • geom_point_svg() for plotting points with SVG as the glyph (This is a direct analogue to geom_point())
  • scale_svg_*() functions for controlling the aesthetic mapping.
    • scale_svg_default() is a sensible default for most plots.
    • scale_svg_* are a shadow set of ggplot2::scale_*() functions with adaptations needed for css() selectors as aesthetics.
    • E.g. scale_svg_fill_brewer() is a direct analogue for ggplot2::scale_fill_brewer()

Installation

Install from GitHub.

The {rsvg} package is used to convert SVG into an R raster object. This requires at least rsvg(>= 2.3.0).

# install.package('remotes')
install.packages('rsvg')
remotes::install_github('coolbutuseless/ggsvg')

Simple plot

svg_url <- 'https://www.svgrepo.com/download/289000/jellyfish.svg'
svg_txt <- paste(readLines(svg_url), collapse = "\n")
grid::grid.draw( svg_to_rasterGrob(svg_txt) )

test_df <- data.frame(
  x = runif(10), 
  y = runif(10), 
  count = sample(3:5, 10, T),
  type  = sample(c('a', 'b', 'c'), 10, T))

test_df
#>             x         y count type
#> 1  0.26550866 0.2059746     3    b
#> 2  0.37212390 0.1765568     5    b
#> 3  0.57285336 0.6870228     3    a
#> 4  0.90820779 0.3841037     3    c
#> 5  0.20168193 0.7698414     3    a
#> 6  0.89838968 0.4976992     3    c
#> 7  0.94467527 0.7176185     4    b
#> 8  0.66079779 0.9919061     3    b
#> 9  0.62911404 0.3800352     3    b
#> 10 0.06178627 0.7774452     4    b

ggplot(test_df) + 
  geom_point_svg(aes(x, y), svg = svg_txt) + 
  theme_bw()

Simple plot with mapped size aesthetic

ggplot(test_df) + 
  geom_point_svg(aes(x, y, size = type), svg = svg_txt) + 
  theme_bw()

Mapping Aesthetics to SVG features with CSS Selectors

Aesthetic values are mapped to SVG features with CSS Selectors.

Snowman SVG

Here is a simple SVG consisting of 2 stacked circles - a big circle on the bottom and a small circle resting on top.

snowman_txt <- '
  <svg viewBox="0 0 100 100 ">
    <circle id="top" cx="50" cy="20" r="20" fill="brown" stroke="black" />
    <circle id="bot" cx="50" cy="70" r="30" fill="brown" stroke="black" />
  </svg>
  '

grid::grid.draw( svg_to_rasterGrob(snowman_txt, width=800, height=800) )

css() helper function

Use the css() helper function to target aesthetics at selected elements within and SVG using css(selector, property = value)

E.g.

  • css("rect.big", stroke = x)
    • Targets <rect> elements with class = "big"
    • Map values in x in data.frame to the SVG stroke property for these targetted elements.

Example

In the following example, two css() selectors are used within the geom_point_svg() call:

  • css("circle#top", fill=type)
    • Targets <circle> elements with id = "top"
    • Map values in type in data.frame to the SVG fill property for these targetted elements.
  • css("circle#bot", stroke='brown')
    • Targets <circle> elements with id = "bot"
    • Set a constant value of brown for the SVG stroke property for these targetted elements.
  • css("circle", 'stroke-width'=10)
    • Targets <circle> elements
    • Set a constant value of 5 for the SVG stroke-wdith property for these targetted elements.

To configure how the variable is mapped to the property on the selected target, you can either use:

  • scale_svg_default() for reasonable defaults
  • scale_svg_*() family of functions
    • Note: the aesthetic argument must match exactly the css(...) call used in the geom_point_svg() call.
snowman_txt <- '
  <svg viewBox="0 0 100 100 ">
    <circle id="top" cx="50" cy="20" r="20" fill="brown" stroke="black" />
    <circle id="bot" cx="50" cy="70" r="30" fill="brown" stroke="black" />
  </svg>
  '


ggplot(test_df) + 
  geom_point_svg(
    aes(x, y, css("circle#top", fill = type)),
    css("circle#bot", stroke = 'brown'),
    css("circle", 'stroke-width'=10),
    svg = snowman_txt
  ) +
  theme_bw() + 
  scale_svg_fill_brewer(aesthetics = css("circle#top", fill = type), palette = 'Dark2')

Acknowledgements

  • R Core for developing and maintaining the language.
  • CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining the repository

More Repositories

1

dplyr-cli

Manipulate CSV files on the command line using dplyr
R
269
star
2

ggrgl

3d extension to ggplot
R
182
star
3

emphatic

Highlighting R output in the console
R
137
star
4

yyjsonr

Fast JSON package for R
C
124
star
5

tickle

Easily create UIs in base R
R
123
star
6

devout

Write R graphics output devices in plain R
C++
97
star
7

anotherworld

AnotherWorld ported to R
R
75
star
8

ggreverse

Reverse a ggplot object back into code
R
67
star
9

eventloop

Event Loop in R
R
65
star
10

isocubes

R
61
star
11

nara

nativeRaster tools for R
C
60
star
12

wordle

Wordle helper for RStats
R
59
star
13

devoutsvg

Bespoke SVG graphics output device with pattern fill support
R
57
star
14

rllama

Minimal R wrapper for llama.cpp
C
55
star
15

carelesswhisper

Automatic speech recognition in R using whisper.cpp
C
54
star
16

svgparser

Render SVG images in R. Load SVG data as data.frames
R
48
star
17

ggthreed

3d geoms and stats for ggplot
R
46
star
18

threed

Three-Dimensional Object Transformations
R
43
star
19

minipdf

Minimal pure-R PDF document creator
R
41
star
20

gluestick

Simple, single-function string interpolation in Base R
R
41
star
21

tr808r

TR-808 Drum Machine for R
R
40
star
22

xxhashlite

Very fast hash functions using xxHash
C++
36
star
23

facetious

Home to some alternate facetting for ggplot2
R
36
star
24

geomlime

ggplot geom_lime()
R
34
star
25

ggblur

Blurry Point Geom for ggplot2
R
34
star
26

fugly

Extract named substrings using named capture groups in regular expressions.
R
34
star
27

rbytecode

R bytecode assembler/disassembler
R
34
star
28

minicss

Build CSS selectors, styles and stylesheets within R
R
31
star
29

minisvg

Create SVG documents with R
R
29
star
30

zstdlite

Fast, configurable in-memory compression of R objects with zstd
C
26
star
31

numberwang

numbers to words and vice versa
R
25
star
32

pacman

Pacman-ish game in R
R
24
star
33

wingspan

Data about the birds and bonus cards in the boardgame "Wingspan"
R
24
star
34

memoisetools

a collection of additional caches and helper functions to work alongside the memoise package
R
23
star
35

terse

Terse output for nested lists and data.frames
R
22
star
36

ransid

Convert images to ANSI with R
R
21
star
37

lz4lite

Very Fast compression/decompression of in-memory numeric vectors with LZ4
C
20
star
38

miranda

Fast PRNGs for R
C
20
star
39

hershey

Hershey vector font data for RStats
R
20
star
40

chipmunkcore

R wrapper around the Chipmunk2d physics simulation library
C
19
star
41

simplercpp

A demo R package incorporating C code with Rcpp
C++
19
star
42

minihtml

A package for building HTML documents in R (shiny compatible)
R
18
star
43

simplecall

A demo R package incorporating C code which is called with .Call()
C
18
star
44

callme

Easily compile inline C code for R
R
17
star
45

foist

Fast Output of Images
C++
17
star
46

RStudioConf-2022

17
star
47

minidrawio

Create simple draw.io documents from R
R
16
star
48

displease

Non-linear numeric interpolation
R
16
star
49

CP1919

Cambridge Pulsar at 19 hours and 19 minutes right ascension
R
16
star
50

purler

Fast run-length encoding with NA support and results as a data.frame
C
16
star
51

fastpng

Read/write 8-bit/16-bit PNGs with rasters, native rasters, numeric+integer arrays, indexed images with palette, packed pixels in raw vector. Configurable compression settings allow for speed/size tradeoff.
C
16
star
52

ggqr

ggplot2 geom for QR codes
R
16
star
53

CRAN-checks

Notes about extra CRAN checks
16
star
54

nonogram

Nonogram solver in rstats
R
15
star
55

optout

Optimized Graphics Output
R
15
star
56

ingrid

Tools for interactive grid creation and manipulation in the console
R
15
star
57

gridfont

A version of the 'gridfont' vector font in an R friendly format
R
15
star
58

phon

Tools and Data for the CMU Pronouncing Dictionary
R
14
star
59

rd2list

Convert Rd documentation to a structured, human-readable list
R
14
star
60

flagon

Flags of the World
R
14
star
61

chipmunkbasic

Higher-level R6 wrapper around Chipmunk2d rigid body physics simulation library
R
14
star
62

anon

Anonymous function creation in R and purrr
R
14
star
63

devoutdrawio

R graphics device to output to draw.io XML vector format
R
14
star
64

miniverse

A constellation of packages for document creation in R
R
14
star
65

devoutaudio

An R graphics device which renders to audio
R
13
star
66

image2xlsx

Convert an image to an excel spreadsheet
R
13
star
67

strictlyr

Stricter subset of dplyr
R
13
star
68

insitu

In-place modification of vectors
C
13
star
69

svgpatternusgs

SVG patterns from the U.S. Geological Survey for use within R
R
13
star
70

cstructr

Exposing C structrs to R
C
13
star
71

ggecho

ggplot2 stat for echoing data
R
13
star
72

arcadefont

Oldschool arcade vector font
R
13
star
73

devoutrgl

R graphics device to render to {rgl}
R
13
star
74

rmonocypher

Easy to use encryption tools for R
C
13
star
75

rconnection

Writing a custom connection for R
C
13
star
76

cgrep

Highlighted grep of R objects
R
12
star
77

serializer

Example showing how to access R's serialization functions from C
C
12
star
78

getrect

Partition matrix into single-valued rectangular areas
R
12
star
79

smallfactor

An R factor backed by a raw vector rather than an integer vector.
R
11
star
80

visvalingam

R package for Visvalingam Line Simplification
C
11
star
81

analemmatic

Create analemmatic sundials with R
R
11
star
82

cairocore

Low-level bindings to the CairoGraphics library for fast 2d drawing operations
R
11
star
83

bdftools

Bitmap font tools for R
R
11
star
84

simplefortran

Demo of how Fortran code could be included in an R package
C
10
star
85

svgpatternsimple

Create some simple repeating SVG patterns in R
R
10
star
86

naratext

Render text as nativeRaster images
R
10
star
87

snowcrash

Encode arbitrary objects as PNGs, rasters and rasterGrobs
R
9
star
88

frak

Fractal Generator
C
9
star
89

grrr

Modify function default arguments
R
9
star
90

triangular

Decompose complex polygons into sets of triangles
R
9
star
91

devoutansi

RStats ANSI graphics device
R
9
star
92

codespacer

Setup CodeSpace with RStudio support
Dockerfile
9
star
93

cryogenic

Freezing calls, modifying arguments and evaluating later
R
8
star
94

btnvips

libvips cli wrapper for #RStats (btn = better than nothing)
R
8
star
95

colourlookup

Technical demonstration of hash lookup for R colour names
C
8
star
96

ggdebug

Package to help debug and inspect ggplot stats
R
8
star
97

simplec

Demo R package with C code which is called using ".C()'
C
8
star
98

simpletrie

Simple trie in pure R
R
8
star
99

poissoned

Poisson Disc Sampling in R
R
8
star
100

devoutpdf

A hand-crafted PDF graphics output device written in plain R
R
8
star