Q-Q plot with ggplot2::stat_qq, colours, single group

前端 未结 2 2178
心在旅途
心在旅途 2020-12-07 01:29

I\'m looking for a more convenient way to get a Q-Q plot in ggplot2 where the quantiles are computed for the data set as a whole. but I can use mappings (colour

2条回答
  •  独厮守ぢ
    2020-12-07 01:50

    You could calculate the quantiles yourself and then plot using geom_point:

    dda = cbind(dda, setNames(qqnorm(dda$.resid, plot.it=FALSE), c("Theoretical", "Sample")))
    
    ggplot(dda) + 
      geom_point(aes(x=Theoretical, y=Sample, colour=f))
    

    Ah, I guess I should have read to the end of your question. This is the manual solution you were referring to, right? Although you could just package it as a function:

    my_stat_qq = function(data, colour.var) {
    
      data=cbind(data, setNames(qqnorm(data$.resid, plot.it=FALSE), c("Theoretical", "Sample")))
    
      ggplot(data) + 
        geom_point(aes_string(x="Theoretical", y="Sample", colour=colour.var))
    
    }
    
    my_stat_qq(dda, "f")
    

提交回复
热议问题