ggplot: adding normal distribution curve using stat_function to existing histogram and distribution layers

funykcheff

Task that I need to accomplish: 1. draw x=data/y=density histogram - done 2. draw distribution curve for given dataset - done 3. draw perfect normal distribution curve for this dataset (red line) - problem I assume problem is in 2nd stat_function.

Code is run-able w/o any preparations:

data <- data.frame(c(runif(30,1,50)),c(runif(30,50,1)))
g.data <- data[,1]

graph <- ggplot(data, aes(g.data))
graph <- graph +
geom_histogram(aes(y = ..density..), binwidth = 2, fill = 'pink') +
labs(x = 'Data', y ='Density') +
stat_function(fun = dnorm, args = list(mean = mean(g.data, na.rm = T),
sd = sd(g.data, na.rm =T)), colour ='black', size =1) +
theme(legend.position = 'none') +
stat_function(fun = dnorm, colour = "red", args = list(mean = mean(g.data)))
graph

Here what I get

Here what I approximately need, perfect norm. distribution

DeveauP

A normal distribution has 2 parameters: its mean and standard deviation. Here your provide only the mean to dnorm, so it assumes sd = 1.

A corrected version of the code you provide is:

data <- data.frame(c(runif(30,1,50)))
ggplot(data, aes(data[,1])) +
    geom_histogram(aes(y = ..density..), binwidth = 2, fill = 'pink') +
    labs(x = 'Data', y = 'Density') +
    stat_function(fun = dnorm, 
        args = list(mean = mean(data[,1], na.rm = TRUE), 
                    sd = sd(data[,1], na.rm = TRUE)), 
        colour = 'black', size = 1) 

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Plotting the poisson distribution using ggplot2's stat_function

From Dev

How to draw a Normal Distribution Curve (Bell curve) using CSS and JavaScript?

From Dev

Trying to add normal distribution curve to ggplot, and it's not working

From Dev

Superimpose a normal distribution to a density using ggplot in R

From Dev

coloring a segments of a curve when using stat_function in ggplot

From Dev

Adding a legend to an histogram plot that has several stat_bin layers using ggplot2

From Dev

Compare Histogram to Poisson Distribution and Gauss-Curve

From Dev

Adding a legend to ggplot when using stat_function

From Dev

Plot density curve of mixture of two normal distribution

From Dev

Draw histogram with normal distribution overlay from data

From Dev

ggplot add Normal Distribution while using `facet_wrap`

From Dev

ggplot2: normal distribution curve at bottom of graph - geom_text() + geom_historigram()

From Dev

Plotting normal curve over histogram using ggplot2: Code produces straight line at 0

From Dev

Fitting a curve to weibull distribution in R using nls

From Dev

JavaScript Math.random Normal distribution (Gaussian bell curve)?

From Dev

Fitting a distribution given the histogram using scipy

From Dev

Fit bimodal normal distribution to a vector of values, not its histogram

From Dev

Fit Poisson distribution to normal distribution

From Dev

Integration of normal distribution using Simpson's rule

From Dev

Calculate Proportion in R using Normal Distribution

From Dev

Generating matrix with normal distribution using 'sapply', in R

From Dev

R Creating Normal Distribution Plot using dataset

From Dev

draw from skew normal distribution using stan

From Dev

Creating a histogram with distribution curve, where the curve series is larger than the bin series

From Dev

Fitting distribution on curve

From Dev

fitting a distribution to survival curve

From Dev

Fit a distribution to a histogram

From Dev

Histogram - Grade Distribution

From Dev

The reverse/inverse of the normal distribution function in R

Related Related

  1. 1

    Plotting the poisson distribution using ggplot2's stat_function

  2. 2

    How to draw a Normal Distribution Curve (Bell curve) using CSS and JavaScript?

  3. 3

    Trying to add normal distribution curve to ggplot, and it's not working

  4. 4

    Superimpose a normal distribution to a density using ggplot in R

  5. 5

    coloring a segments of a curve when using stat_function in ggplot

  6. 6

    Adding a legend to an histogram plot that has several stat_bin layers using ggplot2

  7. 7

    Compare Histogram to Poisson Distribution and Gauss-Curve

  8. 8

    Adding a legend to ggplot when using stat_function

  9. 9

    Plot density curve of mixture of two normal distribution

  10. 10

    Draw histogram with normal distribution overlay from data

  11. 11

    ggplot add Normal Distribution while using `facet_wrap`

  12. 12

    ggplot2: normal distribution curve at bottom of graph - geom_text() + geom_historigram()

  13. 13

    Plotting normal curve over histogram using ggplot2: Code produces straight line at 0

  14. 14

    Fitting a curve to weibull distribution in R using nls

  15. 15

    JavaScript Math.random Normal distribution (Gaussian bell curve)?

  16. 16

    Fitting a distribution given the histogram using scipy

  17. 17

    Fit bimodal normal distribution to a vector of values, not its histogram

  18. 18

    Fit Poisson distribution to normal distribution

  19. 19

    Integration of normal distribution using Simpson's rule

  20. 20

    Calculate Proportion in R using Normal Distribution

  21. 21

    Generating matrix with normal distribution using 'sapply', in R

  22. 22

    R Creating Normal Distribution Plot using dataset

  23. 23

    draw from skew normal distribution using stan

  24. 24

    Creating a histogram with distribution curve, where the curve series is larger than the bin series

  25. 25

    Fitting distribution on curve

  26. 26

    fitting a distribution to survival curve

  27. 27

    Fit a distribution to a histogram

  28. 28

    Histogram - Grade Distribution

  29. 29

    The reverse/inverse of the normal distribution function in R

HotTag

Archive