0

Want facet_wrap to have different parameters for each plot. Example below:

x = c(43,22,53,21,13,53,23,12,32)
y = c(42,65,23,45,12,22,54,32,12)

df = cbind(x,y)

df = as.data.frame(df)  

meany = mean(y)

p = ggplot(df, aes(x=x,y=y, colour=(y > meany))) + 
      geom_point() +
      geom_hline(yintercept = meany)
p

That works fine, there is a line at mean of y and the points are different colors above and below the line.

I have a larger data frame where I want to do this to each factor level and use facet_wrap to show all the plots. I am not sure how to get the colour and yintercept in ggplot to change for every graph in facet_wrap.

Also, I want the plot to have more layers, i.e. each plot will be comparing MSE of different models.

Thanks for the help.

user1807096
  • 25
  • 1
  • 2
  • 7
  • Do you have a small subset of your actual `data.frame`? Did you read http://docs.ggplot2.org/0.9.3.1/facet_wrap.html? Does `p + facet_wrap(~ yourFactor)` already bring the desired result? Please read also http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Beasterfield May 17 '13 at 12:55

1 Answers1

2

Something like this?

DB <- data.frame(F = factor(rep(LETTERS[1:3], each = 20)),
                 x = rnorm(60, 40, 5), 
                 y = rnorm(60, 40, 5))

library(plyr)
library(ggplot2)

DB <- ddply(DB, "F", mutate, ind = y > mean(y))
mns <- ddply(DB, "F", summarise, meany = mean(y))

ggplot(DB, aes(x = x, y = y, color = ind)) +
    geom_point() +
    geom_hline(data = mns, aes(yintercept = meany)) +
    facet_wrap(~ F, nrow = 1)

Your last request is too vague (i.e., the context is lacking).

Dennis
  • 732
  • 4
  • 4