用ggplot2 :: stat_qq绘制Q-Q图,颜色,单组 [英] Q-Q plot with ggplot2::stat_qq, colours, single group

查看:1782
本文介绍了用ggplot2 :: stat_qq绘制Q-Q图,颜色,单组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更方便的方法来获得Q $ Q图,在 ggplot2 中分位数是作为整体为数据集计算的。但我可以在数据中使用映射(颜色/形状)。

  library(dplyr)
library( ggplot2)
库(扫帚)## for augment()

弥补一些数据:

  set.seed(1001)
N < - 1000
G < - 10
dd < - data_frame(x = runif(N),
f = factor(sample(1:G,size = N,replace = TRUE)),
y = rnorm(N)+ 2 * x + as.numeric(f))
m1 <-lm(y_x,data = dd)
dda <-cbind(augment(m1),f = dd $ f)

基本图:

  ggplot(dda)+ stat_qq(aes(sample = .resid))



如果我尝试添加颜色,那么这些组为分位计算(我不想要)分隔开来: p>

  ggplot(dda)+ stat_qq(aes(sample = y,color = f))



如果我使用 stat_qq(aes(sample = y,color = f,group = 1)) ggplot会忽略颜色规格,并返回第一个绘图。 / p>

我想要一个情节,其中的点位置与第一种情况相同,但是与第二种情况相同。我有一个基于qqnorm 的手动解决方案,我可以发布,但我正在寻找更好的东西...

你可以自己计算分位数,然后使用 geom_point 进行绘图:

<$ p $解析方案

($ d $,$ .resid,plot.it = FALSE),c(理论,样本)))

(dda,setNames ggplot(dda)+
geom_point(aes(x = Theoretical,y = Sample,color = f))



啊,我想我应该读完你的问题。这是你所指的手动解决方案,对吧?虽然您可以将它打包为一个函数:

  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,color = colour.var))

}

my_stat_qq(dda,f )


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/shapes) for groups in the data.

library(dplyr)
library(ggplot2)
library(broom) ## for augment()

Make up some data:

set.seed(1001)
N <- 1000
G <- 10
dd <- data_frame(x=runif(N),
             f=factor(sample(1:G,size=N,replace=TRUE)),
             y=rnorm(N)+2*x+as.numeric(f))
m1 <- lm(y~x,data=dd)
dda <- cbind(augment(m1),f=dd$f)

Basic plot:

ggplot(dda)+stat_qq(aes(sample=.resid))

if I try to add colour, the groups get separated for the quantile computation (which I don't want):

ggplot(dda)+stat_qq(aes(sample=y,colour=f))

If I use stat_qq(aes(sample=y,colour=f,group=1)) ggplot ignores the colour specification and I get the first plot back.

I want a plot where the points are positioned as in the first case, but coloured as in the second case. I have a qqnorm-based manual solution that I can post but am looking for something nicer ...

解决方案

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

这篇关于用ggplot2 :: stat_qq绘制Q-Q图,颜色,单组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆