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:
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?
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)
plot(df$b,df$a)
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)