1

Solution found.

Issue: Using ggplot in R, I am creating histograms with density curves and would like to add some calculated statistics to the plot so that I can flip through them and compare later on. I would like to add mean, standard dev, ks.test(), and a few other variables to the plots, preferably up in the top right corner. I am using the annotate function to do this but how can I set the x and y position to always be in the top right corner?

Here is my code that puts the text in a fixed position, which throws off the scale of my plot.

p <- ggplot(err, aes(x = BizTempErrors)) +
geom_histogram(aes(y = ..density..)) +
geom_density(aes(colour = "Kernal"), parse = T) +
stat_function(fun = dnorm, 
              args = list(mean(BizTempErrors), 
                          sd(BizTempErrors)), 
              aes(colour="Normal")) +
annotate("text", x = 500, y = .0011, label = "Stats to be Displayed") +
ggtitle(label = "TITLE")
plot(p)

Solution:

d.BTE <- density(BizTempErrors)  #find max density point to get location for annotations
maxPointY <- max(d.BTE$y)
maxPointX <- max(BizTempErrors)
Uwe
  • 41,420
  • 11
  • 90
  • 134
nick
  • 11
  • 3

0 Answers0