Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
Anonymous
Not applicable

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
Icey
Community Support
Community Support

Hi @Anonymous ,

 

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

6 REPLIES 6
Icey
Community Support
Community Support

Hi @Anonymous ,

 

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.

Why the frequency shown in decimals (x axis)? 

Anonymous
Not applicable

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

Anonymous
Not applicable

Yes thank you so much!!! 🙂 

 

Icey
Community Support
Community Support

Hi @Anonymous ,

 

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.

Anonymous
Not applicable

@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! 

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.