Search code examples
rplotscatter-plot

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


I'm using the plot() function in rstudio for deploying a xy plot... but what's displayed looks like an odd, unsorted boxplot.

Here is the code I use:

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")

And here is what I get:

plot returned in rstudio. Note that the x axis doesn't even look sorted and, overall, it looks more like a box plot than a xy plot.

Note : if I change the order x and y, then the chart is OK.

Does anyone knows why plot() does this and how to get a proper xy plot?


Solution

  • plot defaults to boxplot if the x axis is factor and the y is numeric, and it defaults to xy plot if vice versa. For example:

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

    enter image description here

    plot(df$b,df$a)
    

    enter image description here

    Looking at your x axis, which is not in order, I'm guessing that's your issue. This should fix it:

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