qqline connects the first and third quartiles. How do I draw a line between different quantiles (ie 30% and 70%)?

前端 未结 3 835
南笙
南笙 2021-01-20 14:14

I have a qqnorm plot of a data set with 1000+ points. I want to draw a line between two quantiles at a time (say 30% and 70%) just as qqline does with 25% and 75%, but with

相关标签:
3条回答
  • 2021-01-20 15:05

    I think you just need

    qqline(diffbp,probs=c(0.3,0.7))
    

    edit: I see from the comments on R: qqline connects the first and third quartiles. How do I draw a line between different quantiles (ie 30% and 70%)? that the difference is that I am using R-devel, and (as @caracal points out) that this feature is new since R 2.15.1 patched (see http://developer.r-project.org/blosxom.cgi/R-2-15-branch/NEWS , 7 July 2012)

    Here's an example that seems to show that qqline() works (I used 0.1, 0.9 for greater contrast):

    set.seed(101)
    z <- rgamma(1000,shape=1)
    newprobs <- c(0.1,0.9)
    ## png("qq.png")
    qqnorm(z,pch=".")
    qqline(z,col="red")
    qqline(z,col="blue",probs=newprobs)
    ## add reference lines
    abline(h=quantile(z,c(0.25,0.75)),col="red",lty=2)
    abline(h=quantile(z,newprobs),col="blue",lty=2)
    abline(v=qnorm(c(0.25,0.75)),col="red",lty=2)
    abline(v=qnorm(newprobs),col="blue",lty=2)
    ## dev.off()
    

    enter image description here

    0 讨论(0)
  • 2021-01-20 15:15

    Make a new function by copying the code and changing the second line to:

    x <- qnorm(c(0.30, 0.70))
    
    0 讨论(0)
  • 2021-01-20 15:18

    EDIT:

    The information below is valid for R (and stats package) version 2.15.1 - apparently later versions will incorporate the built-in capability of plotting arbitrary quantiles.


    qqline seems to be hard-coded to plot the .25 and .75 quantiles. But if you don't mind creating your own function, something like this might do:

    myQqplot <- function(data, probs){
      qqnorm(data)
      theQuants <- quantile(data, probs = probs)
      lm1 <- lm(theQuants~qnorm(probs))
      abline(lm1)
      invisible()
    }
    
    myQqplot(diffbp, c(.5,.99))  
    

    enter image description here

    There's no check to make sure that you supply only two quantiles to the probs argument, but you could add it if you want.

    0 讨论(0)
提交回复
热议问题