6

Is there some way how to make custom points in R? I am familiar with pch argument where are many choices, but what if I need to plot for example tree silhouettes? For example if I draw some point as eps. (or similar) file, can I use it in R?. Solution by raster is not good in the case of complicated objects (f.e. trees).

enter image description here

koekenbakker
  • 3,524
  • 2
  • 21
  • 30
Ladislav Naďo
  • 822
  • 12
  • 27
  • Hi @docendodiscimus!. I do not think it is a duplicate question. I have edited the question. Hope the image clearly explain why the raster solution is not working. – Ladislav Naďo Jan 20 '15 at 10:45
  • 1
    You haven't explicitly asked for non-`ggplot` solutions. If you can consider `ggplot`, have a look [**here**](http://stackoverflow.com/questions/27637455/is-it-possible-to-display-custom-image-say-png-format-as-geom-point-in-r-ggplo). Also check the suggestion in the comments by me and @joran. – Henrik Jan 20 '15 at 11:21
  • I think this can be done with the `grImport` package, at least for .ps files. You can find some examples [here](http://cran.r-project.org/web/packages/grImport/vignettes/import.pdf), check Figure 8.. – koekenbakker Jan 20 '15 at 12:00

1 Answers1

10

You can do this with the grImport package. I drew a spiral in Inkscape and saved it as drawing.ps. Following the steps outlined in the grImport vignette, we trace the file and read it as a sort of polygon.

setwd('~/R/')
library(grImport)
library(lattice)

PostScriptTrace("drawing.ps") # creates .xml in the working directory
spiral <- readPicture("drawing.ps.xml")

The vignette uses lattice to plot the symbols. You can also use base graphics, although a conversion is needed from device to plot coordinates.

# generate random data
x = runif(n = 10, min = 1, max = 10)
y = runif(n = 10, min = 1, max = 10)

# lattice (as in the vignette)
x11()
xyplot(y~x,
       xlab = "x", ylab = "y",
       panel = function(x, y) {
        grid.symbols(spiral, x, y, units = "native", size = unit(10, "mm"))
        })

# base graphics
x11()
plot(x, y, pty = 's', type = 'n', xlim = c(0, 10), ylim = c(0, 10))
xx = grconvertX(x = x, from = 'user', to = 'ndc')
yy = grconvertY(y = y, from = 'user', to = 'ndc')
grid.symbols(spiral, x = xx, y = yy, size = 0.05)

enter image description here

koekenbakker
  • 3,524
  • 2
  • 21
  • 30