How can I easily plot a Chi-Square distribution in R?

The process of plotting a Chi-Square distribution in R can be easily achieved by following a few simple steps. First, the user must load the necessary package for statistical computations, such as “stats.” Next, the user can use the “rchisq” function to generate a random sample of the Chi-Square distribution with a specified degree of freedom. Then, the “hist” function can be used to create a histogram of the generated sample. Finally, the user can use the “curve” function to overlay the theoretical Chi-Square distribution curve on the histogram, providing a visual representation of the distribution. This straightforward process allows for the easy visualization of the Chi-Square distribution in R.

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%

x