r - 轴错误(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”指定的值无效
如何纠正这个错误?
解决方案
我在下面有一个建议,但首先我看到两个问题:
- 您正在加载两个包,
fpc
和dbscan
,它们都有不同的函数,名为dbscan()
. 这可能会在以后产生棘手的错误(例如,如果您更改加载包的顺序,将运行不同的功能)。 - 目前尚不清楚您要绘制什么,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")
这是它生成的图像:
如果您正在寻找其他东西,请更具体地说明最终的情节应该是什么样子。
推荐阅读
- python-3.x - 如何匹配两个 DataFrame 中的特定值并在 Python Pandas 中添加额外的列?
- python - 带有 chunksize 参数的 pandas read_csv 会产生一个只能使用一次的可迭代对象?
- algorithm - 科曼练习:随机(a,b)
- javascript - 永无止境的滚动,两侧克隆项目
- c# - 将数据从表格 B 传递到表格 A 不起作用
- mysql - 基于分组的自动增量
- javascript - 如何将 eventListener 应用于具有相同类的多个元素并让每个元素独立响应
- c# - 在 c# 中使用 IEnumerable 和 List 的 Groupby 错误
- ruby - 斐波那契数列红宝石
- java - 如何使用 REST API 为每个用户创建不同的资源