在R中的卡方QQ图中绘制qqline

时间:2019-03-30 19:12:14

标签: r plot quantile

我将以下代码复制为r help。但是,qqline函数不起作用。是什么原因?

library(mgcViz)
y <- rchisq(500, df = 3)
## Q-Q plot for Chi^2 data against true theoretical distribution:
qqplot(qchisq(ppoints(500), df = 3), y, main = expression("Q-Q plot for" ~~ {chi^2}[nu == 3]))
qqline(y, distribution = function(p) qchisq(p, df = 3), prob = c(0.1, 0.6), col = 2)

谢谢!

1 个答案:

答案 0 :(得分:1)

要使用mgcViz添加qqline,您需要使用ggplot:

library(mgcViz)
y <- rchisq(500, df = 3)
## Q-Q plot for Chi^2 data against true theoretical distribution:
qqplot(qchisq(ppoints(500), df = 3), y, main = expression("Q-Q plot for" ~~ {chi^2}[nu == 3]))

# Add qq line
library(ggplot2)
ggplot2::last_plot() + qqline(y, distribution = function(p) qchisq(p, df = 3), prob = c(0.1, 0.6), col = 2)

qq plot

要查看qq线的截距和斜率,可以执行以下操作:

my.qqline = qqline(y, distribution = function(p) qchisq(p, df = 3), prob = c(0.1, 0.6), col = 2)
> my.qqline$data
   intercept     slope
1 0.08606285 0.9290453