Creating a facet_wrap plot with ggplot2 with different annotations in each plot

To fix the second problem use

annotate("text", 0.375, -1.25,
         label=paste("rho==", round(cor(abm.data$area, abm.data$U1_2), 2)),
         parse=TRUE)

i.e. "rho==".

Edit: Here is a solution to solve the first problem

library("plyr")
library("ggplot2")

set.seed(1)
df <- data.frame(x=rnorm(300), y=rnorm(300), cl=gl(3,100))   # create test data
df.cor <- ddply(df, .(cl), function(val) sprintf("rho==%.2f", cor(val$x, val$y)))

p1 <- ggplot(data=df, aes(x=x)) +
             geom_point(aes(y=y, colour="col1", alpha=0.4)) +
             facet_wrap(~ cl, ncol=3) +
             geom_text(data=df.cor, aes(x=0, y=3, label=V1), parse=TRUE) +
             scale_colour_manual(values=c("col1"="red")) +
             opts(legend.position="none")
print(p1)

The same question may be asked as for adding segments for each facet. We can solve these general problems by geom_segment instead of annotate("segment",...), for the geom_foo, we can define a data.frame to store the data for the geom_foo.

Tags:

Graph

R

Ggplot2