r plot() 绘制箱形图而不是 xy 图

r plot() plots a box plot instead of a xy plot

我在 rstudio 中使用 plot() 函数来部署 xy 图...但是显示的内容看起来像一个奇怪的、未排序的箱线图。

这是我使用的代码:

file = "/Users/Mike/OneDrive - Quantium/Training/Stanford/Chapter 2/Auto.csv"
Auto=read.csv(file)
plot(Auto$horsepower
     , Auto$mpg
     , xlab="Horsepower"
     , ylab="MPG"
     , type="p")

这是我得到的:

注意:如果我更改 x 和 y 的顺序,那么图表就可以了。

有谁知道为什么 plot() 这样做以及如何获得正确的 xy 图?

如果 x 轴是因子且 y 是数字,则

plot 默认为箱线图,反之则默认为 xy 图。例如:

df<-data.frame(a=letters[c(1:5,1:5)],b=c(1:5,11:15))
plot(df$a,df$b)

plot(df$b,df$a)

看看你的 x 轴,它的顺序不对,我猜这是你的问题。 这应该可以解决它:

Auto$horsepower<-as.integer(Auto$horsepower)