ian ian - 1 month ago 6x
R Question

ggplot2: separate color scale per facet

Intuitively I'm looking for something like:


I do something like

p <- ggplot(mpg, aes(year, displ, color=model)) + facet_wrap(~manufacturer)
p + geom_jitter()

That is: plot 2d measurements from individuals(
) belonging to different species(
) faceted by a species, indicating the individual by color.
The problem is that all individuals share the same color scale - so that the points in a facet have very similar colors.

Using the group aesthetic with geom_line would solve the problem, but lines tell different story than dots.

Another obvious solution would be to drop the faceting and draw a separate plot for each subset. (If this should be the only solution: are there any quick, smart or proven ways to do that?)


I'm not sure that this is an available option when you're colouring by a factor. However, a quick way to produce the individual plots would be something like this:

d_ply(mpg, .(manufacturer), function(df) {
jpeg(paste(df$manufacturer[[1]], ".jpeg", sep=""))
plots <- ggplot(df, aes(year, displ, color=factor(model))) + geom_jitter()

Related Answers: Different legends and fill colours for facetted ggplot?