How to Easily Plot a Chi-Square Distribution in R


To create a density plot for a Chi-square distribution in R, we can use the following functions:

  • dchisq() to create the probability density function
  • curve() to plot the probability density function

All we need to do to create the plot is specify the degrees of freedom for dchisq() along with the to and from points for curve().

For example, the following code illustrates how to create a density plot for a chi-square distribution with 10 degrees of freedom where the x-axis of the plot ranges from 0 to 40:

curve(dchisq(x, df = 10), from = 0, to = 40)

Chi-square distribution plot in R with 5 degrees of freedom

Modifying the Density Plot

We can also modify the density plot by adding a title, changing the y-axis label, increasing the line width, and modifying the line color:

curve(dchisq(x, df = 10), from = 0, to = 40,
      main = 'Chi-Square Distribution (df = 10)', #add title
      ylab = 'Density', #change y-axis label
      lwd = 2, #increase line width to 2
      col = 'steelblue') #change line color to steelblue

Chi-square density plot in R

Filling in the Density Plot

In addition to creating the density plot, we can fill in part of the plot using the polygon() function based on a starting and ending value.

The following code illustrates how to fill in the portion of the density plot for the x values ranging from 10 to 40:

#create density curve
curve(dchisq(x, df = 10), from = 0, to = 40,
main = 'Chi-Square Distribution (df = 10)',
ylab = 'Density',
lwd = 2)

#create vector of x values
x_vector <- seq(10, 40)

#create vector of chi-square density values
p_vector <- dchisq(x_vector, df = 10)

#fill in portion of the density plot from 0 to 40
polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))),
        col = adjustcolor('red', alpha=0.3), border = NA)

Chi-square distribution plot in R with values filled in

The following code illustrates how to fill in the portion of the density plot for the x values ranging from 0 to 10:

#create density curve
curve(dchisq(x, df = 10), from = 0, to = 40,
main = 'Chi-Square Distribution (df = 10)',
ylab = 'Density',
lwd = 2)

#create vector of x values
x_vector <- seq(0, 10)

#create vector of chi-square density values
p_vector <- dchisq(x_vector, df = 10)

#fill in portion of the density plot from 0 to 10
polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))),
        col = adjustcolor('red', alpha=0.3), border = NA)

Chi-square distribution with 10 degrees of freedom plot

The following code illustrates how to fill in the portion of the density plot for the x values lying outside of the middle 95% of the distribution:

#create density curve
curve(dchisq(x, df = 10), from = 0, to = 40,
main = 'Chi-Square Distribution (df = 10)',
ylab = 'Density',
lwd = 2)

#find upper and lower values for middle 95% of distribution
lower95 <- qchisq(.025, 10)
upper95 <- qchisq(.975, 10)

#create vector of x values
x_lower95 <- seq(0, lower95)

#create vector of chi-square density values
p_lower95 <- dchisq(x_lower95, df = 10)

#fill in portion of the density plot from 0 to lower 95% value
polygon(c(x_lower95, rev(x_lower95)), c(p_lower95, rep(0, length(p_lower95))),
        col = adjustcolor('red', alpha=0.3), border = NA)

#create vector of x values
x_upper95 <- seq(upper95, 40)

#create vector of chi-square density values
p_upper95 <- dchisq(x_upper95, df = 10)

#fill in portion of the density plot for upper 95% value to end of plot
polygon(c(x_upper95, rev(x_upper95)), c(p_upper95, rep(0, length(p_upper95))),
        col = adjustcolor('red', alpha=0.3), border = NA)

Chi-square distribution with 95% of values outside of distributionLastly, the following code illustrates how to fill in the portion of the density plot for the x values lying inside of the middle 95% of the distribution:

#create density curve
curve(dchisq(x, df = 10), from = 0, to = 40,
main = 'Chi-Square Distribution (df = 10)',
ylab = 'Density',
lwd = 2)

#find upper and lower values for middle 95% of distribution
lower95 <- qchisq(.025, 10)
upper95 <- qchisq(.975, 10)

#create vector of x values
x_vector <- seq(lower95, upper95)

#create vector of chi-square density values
p_vector <- dchisq(x_vector, df = 10)

#fill in density plot
polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))),
        col = adjustcolor('red', alpha=0.3), border = NA)

Chi-square distribution middle 95%

Featured Posts

3 Replies to “How to Easily Plot a Chi-Square Distribution in R”

  1. Hi Zach,
    I have just spent about an hour trying to graph a chi sq distribution with shading as in your notes. I get individual curves but can’t get the shading. I have followed your commands exactly. I am using RStudio. I keep getting the following error:
    Error in xy.coords(x, y, setLab = FALSE) : ‘x’ and ‘y’ lengths differ
    Have you any idea what might be the problem?
    Thanks
    Lynette

  2. Hi, how can I plot a vertical abline for left tail test with a level of significance of 0.025 with df=21= chi-quare 35.479?
    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *