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

前端 未结 2 473
予麋鹿
予麋鹿 2020-12-08 05:30

I am using ggplot2 to explore the result of some testing on an agent-based model. The model can end in one of three rounds per realization, and as such I am interested in h

相关标签:
2条回答
  • 2020-12-08 05:48

    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.

    0 讨论(0)
  • 2020-12-08 05:59

    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)
    
    0 讨论(0)
提交回复
热议问题