【问题标题】:How to make custom plot symbols from vector graphics in R如何从R中的矢量图形制作自定义绘图符号
【发布时间】:2015-01-20 09:50:54
【问题描述】:

有什么方法可以在 R 中创建自定义点吗?我熟悉pch 参数,其中有很多选择,但是如果我需要绘制例如树剪影怎么办? 例如,如果我将某个点绘制为 eps。 (或类似)文件,我可以在 R 中使用它吗?。在复杂对象(例如树)的情况下,光栅解决方案并不好。

【问题讨论】:

  • 嗨@docendodiscimus!。我不认为这是一个重复的问题。我已经编辑了这个问题。希望图片能清楚地解释为什么光栅解决方案不起作用。
  • 您没有明确要求非ggplot 解决方案。如果你可以考虑ggplot,看看here。还要检查我和@joran 在 cmets 中的建议。
  • 我认为这可以通过grImport 包来完成,至少对于 .ps 文件是这样。您可以找到一些示例here,查看图 8..

标签: r graphics plot


【解决方案1】:

您可以使用grImport 包来执行此操作。我在 Inkscape 中画了一个螺旋并将其保存为drawing.ps。按照 grImport vignette 中概述的步骤,我们跟踪文件并将其作为一种多边形读取。

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

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

小插图使用格子来绘制符号。您也可以使用基本图形,但需要从设备到绘图坐标的转换。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多