You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R语言scatterplot函数绘制Iris数据集散点图报错求助

Fixing Your Iris Scatterplot Error in RStudio

Hey there! Let's break down what's causing those errors and fix your scatterplot step by step.

What's Going Wrong?

  • First off, the scatterplot() function (from the car package, I assume) doesn't accept a labels parameter directly. Those warnings about "labels is not a graphical parameter" are telling you exactly that—this function doesn't recognize that argument.
  • The main error ('labels' is supplied and not 'at') happens because R gets confused: when you pass labels, it assumes you're trying to customize axis tick labels, but you didn't provide the corresponding at positions for those labels. That mismatch triggers the error.
  • Also, you mixed up your axis labels! Your formula is Sepal.Width ~ Sepal.Length, which means the x-axis is Sepal.Length and y-axis is Sepal.Width, but your xlab and ylab are reversed. That would have made your plot misleading even if it worked.

Corrected Code

First, make sure you've loaded the car package (since that's where scatterplot() lives):

library(car)

Then, let's fix the scatterplot and add the row name labels correctly:

# Fix axis labels and remove the unsupported labels parameter
scatterplot(Sepal.Width ~ Sepal.Length | Species, data = iris,
            xlab = "Sepal Length", ylab = "Sepal Width",
            main = "Iris Data")

# Add row name labels to each point using the text() function
text(iris$Sepal.Length, iris$Sepal.Width, labels = row.names(iris), cex = 0.7)

Extra Tips

  • The cex = 0.7 in text() makes the labels smaller so they don't overlap too much. Adjust this value if needed (bigger numbers = larger labels).
  • If you don't want labels on every point (they can get cluttered!), you can filter to label only specific groups. For example, to label just the setosa points:
# Filter setosa data
setosa_data <- iris[iris$Species == "setosa", ]
# Add red labels for setosa points
text(setosa_data$Sepal.Length, setosa_data$Sepal.Width, 
     labels = row.names(setosa_data), cex = 0.7, col = "red")

内容的提问来源于stack exchange,提问作者Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:33:14