如何从R中的矢量图形制作自定义绘图符号 [英] How to make custom plot symbols from vector graphics in R
问题描述
有没有办法在R中创建自定义点?我很熟悉 pch
参数,其中有很多选择,但如果我需要绘制树形图例子,该怎么办?
例如,如果我将某点指定为eps。 (或类似的)文件,我可以在R中使用它吗?。对于复杂对象(fe trees),栅格解决方案不好。
grImport来完成这项工作。
包。我在Inkscape中画了一个螺旋线并将它保存为 drawing.ps
。按照grImport 插图中概述的步骤,我们会跟踪该文件并将其作为多边形读取。 setwd('〜/ R /')
library( grImport)
库(格子)
PostScriptTrace(drawing.ps)#在工作目录中创建.xml
螺旋< - readPicture(drawing.ps.xml )
小插图使用点阵来绘制符号。您也可以使用基本图形,但需要从设备转换到绘图坐标。
#生成随机数据
x = runif(n = 10,min = 1,max = 10)
y = runif(n = 10,min = 1,max = 10)
#
x11()
xyplot(y〜x,
xlab =x,ylab =y,
panel = function(x,y){
grid.symbols(spiral,x,y,units =native,size = unit(10,mm))
})
#基本图形
x11( )
plot(x,y,pty ='s',type ='n',xlim = c(0,10),ylim = c(0,10))
xx = grconvertX(x = x,从='user',到='ndc')
yy = grconvertY(y = y,from ='user',to ='ndc')
grid.symbols(spiral,x = xx,y = yy,size = 0.05)
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).
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)
这篇关于如何从R中的矢量图形制作自定义绘图符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!