首页 > 解决方案 > 为什么在 hexbin 对象上调用 graphics::plot 会引发错误?

问题描述

简而言之:我正在尝试运行这个 hexbin 示例,除非我替换plotgraphics::plot. 后者引发以下错误:

> graphics::plot(bin, main="" , colramp=my_colors , legend=F )
Error in as.double(y) :
  cannot coerce type 'S4' to vector of type 'double'

如何使这项工作?

更广泛的背景为什么我需要以这种方式工作。我为使用 RServe 将 R 引入 Clojure的clojisr 项目做出了贡献。有一个选项可以要求 R 包并创建相应的 Clojure 函数。这很好用(见thisthis)。对 R 的底层调用以{package::symbol}for Everything 的形式出现。

标签: rplot

解决方案


Classhexbin是一个 S4 类,包定义了一个 S4 泛型和一个用于plot. (源代码在这里)。plot包命名空间中没有 S4 泛型graphics,只有 S3 泛型。

因此解决方案非常简单:

hexbin::plot(bin, main="" , colramp=my_colors , legend=F ) 

这是一个证明它的代表:

library(hexbin)
library(RColorBrewer)

# Create data
x <- rnorm(mean=1.5, 5000)
y <- rnorm(mean=1.6, 5000)

# Make the plot
bin<-hexbin(x, y, xbins=40)
my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))
hexbin::plot(bin, main="" , colramp=my_colors , legend=F ) 

reprex 包于 2020-02-17 创建(v0.3.0)


推荐阅读