cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted

R Script - Overlay Normal Distribution Curve

Hi everyone, 

 

Having a bit of an issue overlaying a normal distribution curve on a histogram visual, using R scripts. 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(Ratings)
# dataset <- unique(dataset)

# Paste or type your script code here:
library(ggplot2)
ggplot(data=dataset,aes(x=dataset$Ratings)) +
    geom_histogram(aes(y=..density..),col="#ffffff",fill="#bfd730") +
    labs(title="Employee 2019 Performance Rating", x="Performance Ratings", y="Frequency") +
    geom_density(col=2) 

#Adding Normal Curve 
ratings_norm_curve=rnorm(length(Ratings),mean(Ratings),sd(Ratings))

#Adding it to Histogram 
lines(density(ratings_norm_curve, adjust = 2), col="blue", lwd=2)

What might I be doing wrong here?  

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Support
Community Support

Re: R Script - Overlay Normal Distribution Curve

Hi @GradStudent2020 ,

 

Sorry to reply late. Please check whether this could meet your requirements:

library(ggplot2)
ggplot(data=dataset,aes(x=dataset$Ratings)) +
    geom_histogram(aes(y=..density..),col="#ffffff",fill="#bfd730") +
    labs(title="Employee 2019 Performance Rating", x="Performance Ratings", y="Frequency") +
    geom_density(col=2) +
    stat_function(fun = dnorm, 
        args = list(mean = mean(dataset$Ratings, na.rm = TRUE), 
                    sd = sd(dataset$Ratings, na.rm = TRUE)), 
                    colour = 'blue') 

histogram.PNG

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
Highlighted
Community Support
Community Support

Re: R Script - Overlay Normal Distribution Curve

Hi @GradStudent2020 ,

 

Please share me some dummy sample data, not real data, for test.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Highlighted

Re: R Script - Overlay Normal Distribution Curve

@Icey  apologies for the delay, but you I curated the dataset for you - it's a sample, and within the spreadsheet I've included the current visual output, the code I'm using, and a desired outcome statement. 🙂 

 

Let me know what you've come up with! Appreciate your assistance with this. 

Thanks Much! 

Highlighted
Community Support
Community Support

Re: R Script - Overlay Normal Distribution Curve

Hi @GradStudent2020 ,

 

Sorry to reply late. Please check whether this could meet your requirements:

library(ggplot2)
ggplot(data=dataset,aes(x=dataset$Ratings)) +
    geom_histogram(aes(y=..density..),col="#ffffff",fill="#bfd730") +
    labs(title="Employee 2019 Performance Rating", x="Performance Ratings", y="Frequency") +
    geom_density(col=2) +
    stat_function(fun = dnorm, 
        args = list(mean = mean(dataset$Ratings, na.rm = TRUE), 
                    sd = sd(dataset$Ratings, na.rm = TRUE)), 
                    colour = 'blue') 

histogram.PNG

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Highlighted

Re: R Script - Overlay Normal Distribution Curve

Yes thank you so much!!! 🙂 

 

Highlighted

Re: R Script - Overlay Normal Distribution Curve

@Icey  is it possible to show the count of frequency on the x-axis of this code?

Helpful resources

Announcements

August Community Highlights

Check out a full recap of the month!

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power BI

Join us for an in-depth look at the new Power BI features and capabilities at the free Microsoft Business Applications Launch Event.

Top Solution Authors
Top Kudoed Authors