0

It has been already asked but I couldn't find the exact answer for this. I am wondering if the following is doable.

I have a bunch of scatter plot data in ggplot2 library and each has different mean and standard deviation, and the number of observations. I would like to print these information on top of each different plot. Therefore, I am calculating and wanting to print those with annotate. What I am doing is as follows:

m_axl5 <- as.character(round(mean(newdata$axl5), 5))
sd_axl5 <- as.character(round(sd(newdata$axl5), 5))
txt_m <- paste ("mean:", m_axl5)
txt_sd <- paste ("stdev:", m_axl5)
txt <-paste (txt_m, txt_sd, sep = '\n')
axl5 <- ggplot (newdata, aes(y=axl5, x= row.names(newdata))) + geom_point(position = position_jitter(w=0.1, h=0), colour= "red")+
    ggtitle("Axle 5 Weight - Match Error Difference") + xlab("") + ylab("Axle 5 Weight Match Error")
axl5 + theme(axis.ticks = element_blank(), axis.text.x = element_blank()) +annotate("text", x = 1, y = 10, label = txt)

Whenever I do this, it gives me the following error:

Error: Incompatible lengths for set aesthetics: label

I couldn't find a way of customizing label that way or annotating a text on ggplot as a result of my research. If someone can help me, or at least guide me if this was done earlier, I'd be more than glad.

Thanks.

kukushkin
  • 312
  • 4
  • 16

1 Answers1

0

Instead of using annotate() you can do some thing like this

plot.title = 'Axle 5 Weight - Match Error Difference'
plot.subtitle = paste(txt_m, txt_sd, sep = '-')

ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), ""))))

reference: Add dynamic subtitle using ggplot

Community
  • 1
  • 1
Veerendra Gadekar
  • 4,452
  • 19
  • 24