1

I have some data (AllPCA) that is divided by site. I have used qplot (PC1, PC2, data=AllPCA, colour=Population, facets=~Population) + scale_colour_manual (values=cbbPalette) to facet a scatterplot of two variables by site.

Example AllPCA:

ID PC1 PC2 Population
Syd1 0.0185 0.0426 Sydney
Was1 0.0167 0.0415 Washington
Rea1 0.0182 0.0431 Reading
Aar1 0.0183 0.0427 Aarhus

This works fine, but only gives the data from each site in each of the windows.

I would like to create the same plot, but keeping the rest of the data in each facetted plot, just greyed out. Can you help?

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
Joni
  • 375
  • 1
  • 11
  • It's quite simple (you only need to duplicate all observations). If you made your example [reproducible](http://stackoverflow.com/a/5963610/1412059) it would be easy to show you how. – Roland Jul 25 '13 at 10:49
  • Apologies, the actual dataset is huge, so not really possible to provide a good example of it. – Joni Jul 25 '13 at 11:00
  • Have you read the FAQ I linked to? Simply make a small toy example. – Roland Jul 25 '13 at 11:05

2 Answers2

1

One way would be to use two geom_point() calls. In first I use data=AllPCA[,-4] - this is your data without column Population and set color="grey". So all points will be plotted in all facets in grey. Then I add second geom_point() with all data and color=Population. This will add only points in facets corresponding to each Population in separate colors (when facet_wrap() is used).

ggplot()+
  geom_point(data=AllPCA[,-4],aes(PC1,PC2),color="grey")+
  geom_point(data=AllPCA,aes(PC1,PC2,color=Population))+
  facet_wrap(~Population)

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
0

Duplicate your data several times:

n <- length(unique(AllPCA[["Population"]]))
dat <- do.call(rbind, rep(list(AllPCA), n))

Create new columns for (a) facetting and (b) colour:

dat[["Population2"]] <- rep(AllPCA[["Population"]], each = n)
dat[["PopulationMatch"]] <- with(dat, Population == Population2)

Plot:

library(ggplot2)
qplot(PC1, PC2, data = dat, colour = PopulationMatch, facets = ~ Population2) + 
  scale_colour_manual(values = c("grey", "black"))

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168