【问题标题】:Error in axis(side = side, at = at, labels = labels, ...) : invalid value specified for graphical parameter "pch"轴错误(side = side,at = at,labels = labels,...):为图形参数“pch”指定的值无效
【发布时间】:2021-11-13 01:47:03
【问题描述】:

我已在 R 中的内置数据集 iris 上应用 DBSCAN 算法。但尝试使用 plot() 可视化输出时出现错误。

以下是我的代码。

library(fpc)
library(dbscan)


data("iris")
head(iris,2)


data1 <- iris[,1:4]
head(data1,2)

set.seed(220)
db <- dbscan(data1,eps = 0.45,minPts = 5)

table(db$cluster,iris$Species)

plot(db,data1,main = 'DBSCAN')

错误:轴错误(边 = 边,在 = 在,标签 = 标签,...): 为图形参数“pch”指定的值无效

如何纠正这个错误?

【问题讨论】:

标签: r visualization dbscan


【解决方案1】:

我有以下建议,但首先我看到两个问题:

  1. 您正在加载两个包,fpc 和 dbscan,它们都具有名为 dbscan() 的不同函数。这可能会在以后产生棘手的错误(例如,如果您更改加载包的顺序,将运行不同的函数)。
  2. 不清楚您要绘制什么,x 轴或 y 轴应该是什么或绘图的类型。函数plot() 通常将一个向量值作为 x 轴,另一个向量作为 y 轴(尽管并非总是如此,请参阅 ?plot),但在这里您传递给它的是 data.frame 和 dbscan对象,它不知道如何处理它。

这是接近它的一种方法,使用ggplot() 制作散点图,使用dplyr 获得一些便利功能:

# load our packages
# note: only loading dbscacn, not loading fpc since we're not using it
library(dbscan)
library(ggplot2)
library(dplyr)

# run dbscan::dbscan() on the first four columns of iris
db <- dbscan::dbscan(iris[,1:4],eps = 0.45,minPts = 5)

# create a new data frame by binding the derived clusters to the original data
# this keeps our input and output in the same dataframe for ease of reference
data2 <- bind_cols(iris, cluster = factor(db$cluster))

# make a table to confirm it gives the same results as the original code
table(data2$cluster, data2$Species)

# using ggplot, make a point plot with "jitter" so each point is visible
# x-axis is species, y-axis is cluster, also coloured according to cluster
ggplot(data2) +
  geom_point(mapping = aes(x=Species, y = cluster, colour = cluster),
             position = "jitter") +
  labs(title = "DBSCAN")

这是它生成的图像:

如果您正在寻找其他内容,请更具体地说明最终的情节应该是什么样子。

【讨论】:

  • It's not clear what you're trying to plot 这就是为什么你应该检查plot的第一个参数的类
  • @Christopher Belanger 我只是想绘制集群,而您的解释是正确的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
相关资源
最近更新 更多