Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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?
Solved! Go to Solution.
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')
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
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')
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)?
@Icey is it possible to show the count of frequency on the x-axis of this code?
Yes thank you so much!!! 🙂
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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
112 | |
97 | |
84 | |
67 | |
60 |
User | Count |
---|---|
150 | |
120 | |
99 | |
87 | |
68 |