How to easily generate a perfectly normal distribution

April 23, 2019

(This article was first published on R on easystats, and kindly contributed to R-bloggers)

Many times, for instance when teaching, I needed to quickly and simply generate a perfectly normally distributed sample to illustrate or show some of its characteristics.

This is now very easy to do with the new bayestestR package, which includes the rnorm_perfect function. This function is very similar to the classic rnorm (same arguments), with the difference that the generated sample is perfectly normal.


bayestestR can be installed as follows:

install.packages("bayestestR")  # Install the package
library(bayestestR)  # Load it
# Generate a perfect sample
x <- rnorm_perfect(n = 100, mean = 0, sd = 1)

# Visualise it

x %>% 
  density() %>%  # Compute density function %>% 
  ggplot(aes(x=x, y=y)) +

We can also easily color some of the parts of the curve, for instance, the observations lying beyond +2 standard deviations.

x %>% 
  density() %>%  # Compute density function %>% 
  mutate(outlier = ifelse(x > 2, "Extreme", "Not extreme")) %>% 
  ggplot(aes(x=x, y=y, fill=outlier)) +
  geom_ribbon(aes(ymin=0, ymax=y)) +

bayestestR and easystats

More details about bayestestR’s features are comming soon, stay tuned 😉

Feel free to let us know how we could further improve this package! Also, note that easystats, the project supporting bayestestR is in active development. Thus, do not hesitate to contact us if you want to get involved 🙂

  • Check out our other blog posts here!

To leave a comment for the author, please follow the link and comment on their blog: R on easystats. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)