3

I would like to plot a nice, 'approaching the limit'-looking normal pdf in ggplot.

I found that to get a very symmetric and clean looking plot, I had to crank up the number of samples to a rather large number; one million creates a great visualization. However, this is pretty slow, especially if I hope to work with Shiny at some point.

df <- data.frame(c(rnorm(1000000)))
ggplot(df, aes(df[1])) + geom_density()

Surely there is a better way to display something close to the ideal normal distribution?

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149

1 Answers1

1

Basically, your code should look like:

 ggplot(data=dataset, aes(dataset$value)) +
      stat_function(fun = dnorm, args = c(mean = mean(dataset$value), sd = sd(dataset$value)))

stat_function uses the dnorm function (to get the density of a normal variable) parses in the mean & median values and plots the normal distribution.

Reference : How dnorm works?

For ggplot stat_function Documentation follow this link Sample : https://github.com/tidyverse/ggplot2/blob/master/R/stat-function.r

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35