Reputation: 25
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.
Upvotes: 0
Views: 1942
Reputation: 762
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).
Upvotes: 2