Plotting Distributions in R

[This article was first published on Jason Bryer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When working with distributions in R, each distribution has four functions, namely:

  • dXXX – density function.
  • rXXX – generate random number from this distribution.
  • pXXX – returns the area to the left of the given value.
  • qXXX – returns the quantile for the given value/area.

Where XXX is the distribution name (e.g. norm, binom, t, etc.).

remotes::install_github('jbryer/VisualStats')

The VisualStats::plot_distributions() function will generate four plots representing the four R distribution functions. For each subplot points correspond to the first parameter of the corresponding function (note the subplot for the random rXXX function does not have points since this simply returns random values from that distribution). The arrows correspond to what that function will return.

library(VisualStats)
data('distributions', package = 'VisualStats')
plot_distributions(dist = 'norm',
                   xvals = c(-1, 0, 0.5),
                   xmin = -4,
                   xmax = 4)

The top two plots (dXXX and rXXX) plot the distribution. The bottom two plots are the cumulative density function for the given distribution. The CDF describes the probability that a random variable (X) will be less than or equal to a specific value (x), written as F(x) = P(X ≤ x). The CDF provides a complete view of a random variable’s distribution by accumulating probabilities up to that point.

plot_distributions(dist = 'binom',
                   xvals = c(1, 3),
                   xmin = 0,
                   xmax = 10,
                   args = list(size = 10, prob = 0.35))

The VisualStats package also has a Shiny application that allows you to interactively plot the 17 distributions available in base R.

Screenshot of the distributions Shiny application
To leave a comment for the author, please follow the link and comment on their blog: Jason Bryer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)