首页 > 解决方案 > 轴错误(side = side,at = at,labels = labels,...):为图形参数“pch”指定的值无效

问题描述

我已经在 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')

错误:轴错误(side = side,at = at,labels = labels,...):为图形参数“pch”指定的值无效

如何纠正这个错误?

标签: rvisualizationdbscan

解决方案


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

  1. 您正在加载两个包,fpcdbscan,它们都有不同的函数,名为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")

这是它生成的图像:

使用 ggplot() 生成的散点图显示由函数 dbscan::dbscan() 聚类的虹膜种类

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


推荐阅读