This shows that 22 rows of data can't be plotted. Since you are learning both you are presumably trying to figure out which is best. Did base R tell you it couldn't plot 22 values? Thought not! That's not because it could (they are non-finite) - it just didn't tell you. OK. Fixes ? 1. Look at the data and understand why. I suspect it's to do with points at zero. In which was filtering those off would help avoid the error. Or you say it's just a warning and carry on.
OK. Legends. Ggplot can't make up it's mind what to call them. Legends and guides. And Ggplot puts them outside the plot by default. Let's start with that.
Add this to the plot
+scale_colour_manual("Legend title", values = c("ECDF" = "red", "blue"))
Now on each of the stat_ add inside the aes() colour="the name of line", and remove the colour e.g.
stat_ecdf(geom = "line", size=0.5, aes(colour="ECDF")) +
ggplot(d1, aes(d)) +
stat_ecdf(geom = "line", size=0.5, aes(colour="ECDF") +
xlim(5, 20) +
stat_function(fun = pnorm, args = list(mean=mean(d), sd=sqrt(var(d))), aes(colour="FUNC") +
ylab("Fn(x)") +
ggtitle("eCDF(d) betwen 5-20")+
scale_colour_manual("Legend title", values = c("ECDF" = "red", "FUNC" = "blue"))
If that works then you can position the legend using:
- theme(legend.position = c(5, .95))