How to make custom plot symbols from vector graphics in R

天大地大妈咪最大 提交于 2019-12-18 13:38:04

问题


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).


回答1:


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)



来源:https://stackoverflow.com/questions/28042220/how-to-make-custom-plot-symbols-from-vector-graphics-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!