简体   繁体   中英

graph a chi-square with 1 degree of freedom with ggplot

I'm doing a hypothesis test and I have to graph the following chi-square $ \chi_{1,0.95}^2$ i tried to do it with ggplot and it doesn't look good. I hope you can help.

library(ggplot2)
df<-1
p<-0.05
gg<-data.frame(x=seq(0,15,0.1))
gg$y<-dchisq(gg$x,df)
p<-ggplot(gg)+geom_path(aes(x,y))
p

Perhaps you are wanting to plot the cdf of the Chi square distribution to demonstrate where it crosses 0.95?

Here's a plot of the cdfs of the Chi Square with 1 to 5 degrees of freedom, and the point where the first one crosses p = 0.95:

library(ggplot2)

gg <- data.frame(x = rep(seq(0,15,0.1), 5), df = rep(1:5, each = 151))
gg$y <-pchisq(gg$x, gg$df)
p <- ggplot(gg) +
      geom_path(aes(x, y, color = factor(df))) +
      geom_hline(aes(yintercept = 0.95), linetype = 2) +
      geom_vline(aes(xintercept = qchisq(0.95, 1)), linetype = 2) +
      geom_point(aes(x = qchisq(0.95, 1), y = 0.95))
p

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM