Search code examples
rplot

If x and y have the same length, do qqplot(x,y) and plot(sort(x),sort(y)) yield the same plot in R?


This is a question about R. Let x and y be two numerical vectors with the same length. Am I right that the commands qqplot(x,y) and plot(sort(x),sort(y)) yield the same plot in R?

I tried two approaches to find the answer:

  1. I defined x = qqnorm(100) and y = qqnorm(100) and in this case the two plots looked equal to me.
  2. I tried to compare the plots with the code qqplot(x,y) == plot(sort(x),sort(y)) and I got the ouput logical(0) in addition to a plot. I am not sure how to interpret the result. In fact I was expecting to get an error message since the functions qqplot and plot yields objects with different classes, namely qqplot returns a list whereas plot returns NULL.

Solution

  • They are plotting the same thing.

    Compare print(qqplot(x,y)) to print(plot(sort(x),sort(y))) and you'll see why the comparison is unequal. The plot function doesn't return a value; it just renders the plot. But the qqplot is returning something, the sorted arrays in a list.

    Btw, you can rename the plot axis labels to make them look the identical: plot(sort(x),sort(y), xlab = "x", ylab = "y")