Search code examples
rplotdistributionprobability-distribution

Plotting the area under the curve of various distributions in R


Suppose I'm trying to find the area below a certain value for a student t distribution. I calculate my t test statistic to be t=1.78 with 23 degrees of freedom, for example. I know how to get the area under the curve above t=1.78 with the pt() function. How can I get a plot of the student distribution with 23 degrees of freedom and the area under the curve above 1.78 shaded in. That is, I want the curve for pt(1.78,23,lower.tail=FALSE) plotted with the appropriate area shaded. Is there a way to do this?


Solution

  • ggplot version:

    ggplot(data.frame(x = c(-4, 4)), aes(x)) +
      stat_function(fun = dt, args =list(df =23)) +
      stat_function(fun = dt,   args =list(df =23),
                    xlim = c(1.78,4),
                    geom = "area") 
    

    enter image description here