Home » How to Easily Plot a Chi-Square Distribution in R

How to Easily Plot a Chi-Square Distribution in R

by Erma Khan

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 #create vector of chi-square density values
p_vector #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 0, 10)

#create vector of chi-square density values
p_vector #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 #create vector of x values
x_lower95 #create vector of chi-square density values
p_lower95 #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 #create vector of chi-square density values
p_upper95 #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 #create vector of x values
x_vector #create vector of chi-square density values
p_vector #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%

Related Posts