Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom plot symbols from vector graphics in R

Tags:

plot

r

graphics

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

like image 605
Ladislav Naďo Avatar asked Jan 20 '15 09:01

Ladislav Naďo


People also ask

How do I plot different symbols in R?

Using special characters as Pch values In the R Language, we can use “+”, “*“,”-“,”. “,”#, “%”, and “o” for plotting different symbols.

How do I add labels to a plot in R?

Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.

How do I use PCH in R?

Change R base plot point shapesThe default R plot pch symbol is 1, which is an empty circle. You can change this to pch = 19 (solid circle) or to pch = 21 (filled circle). To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name).

What does PCH mean in R studio?

Plot character or pch is the standard argument to set the character that will be plotted in a number of R functions. Explanatory text can be added to a plot in several different forms, including axis labels, titles, legends, or a text added to the plot itself.


1 Answers

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

like image 146
koekenbakker Avatar answered Nov 09 '22 06:11

koekenbakker