Reputation: 3
I am asked to plot the density of the residuals of my dataset against the density of a normal distribution.
When I do this with ggplot2
, it shows me my residual density plot, but not the normal distribution. Additionally, two error messages occur:
Removed 134 rows containing non-finite values (stat_density). Removed 403 row(s) containing missing values (geom_path).
Could somebody explain me why the normal plot is not shown?
Please find my code below:
p1 <- ggplot(steak_eaters)+ geom_density(aes(x= resid))
p1 <- p1 + stat_function(fun =dnorm, n=403, args= list(mean= mean(steak_eaters$resid), sd = sd(steak_eaters$resid)), color= "red") + theme_stata()
plot(p1)
Upvotes: 0
Views: 589
Reputation: 17725
I don't have access to your steak_eaters
dataset, but here is an example with the geom_function
function and a built-in dataset:
library(ggplot2)
ggplot(diamonds, aes(x=depth)) +
geom_density(color="blue") +
geom_function(fun=function(x)
dnorm(x,
mean=mean(diamonds$depth),
sd=sd(diamonds$depth)),
color="red")
The messages you copy seem like warning messages rather than error messages. You probably have missing observations or infinite values in the dataset, which get dropped by the ggplot2
plotting mechanism.
Upvotes: 1