1

Am having trouble making my faceted plot only display data, as opposed to displaying facets with no data.

The following code:

p<- ggplot(spad.data, aes(x=Day, y=Mean.Spad, color=Inoc))+
    geom_point()

p + facet_grid(N ~ X.CO2.)

Gives the following graphic: enter image description here

I have played around with it for a while but can't seem to figure out a solution.

Dataframe viewable here: https://docs.google.com/spreadsheets/d/11ZiDVRAp6qDcOsCkHM9zdKCsiaztApttJIg1TOyIypo/edit?usp=sharing

Reproducible Example viewable here: https://docs.google.com/document/d/1eTp0HCgZ4KX0Qavgd2mTGETeQAForETFWdIzechTphY/edit?usp=sharing

Nick Chandler
  • 77
  • 1
  • 10
  • Can you include some sample data, perhaps using the command `dput(head(spad.data))`. – Mist Feb 22 '16 at 03:06
  • Not quite sure how to use that function, as I am fairly new to R. I shared a link to the data instead. I hope that helps – Nick Chandler Feb 22 '16 at 03:15
  • please add data to the question.. – astrosyam Feb 22 '16 at 03:15
  • Any guidance on how to do that? – Nick Chandler Feb 22 '16 at 03:47
  • Run the dput command I gave you, edit your question and then copy and paste the output of the dput into the question. – Mist Feb 22 '16 at 04:28
  • dput output was too large for question I added the reproducible example to a google doc. Worked when I tried it. – Nick Chandler Feb 22 '16 at 04:34
  • 2
    Links can break, and SO-questions are not only for your benefit. As such, they should be understandable within themselves, without outside links for data/code... Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Feb 22 '16 at 08:37
  • remove rows with missing values before plotting: `spad.sample <- spad.sample[complete.cases(spad.sample),]` – erc Feb 22 '16 at 08:40

1 Answers1

2

Your issue lies in the missing observations for your x- and y variables. Those don't influence the creation of facets, that is only influenced by the levels of faceting variables present in the data. Here is an illustration using sample data:

#generate some data
nobs=100
set.seed(123)
dat <- data.frame(G1=sample(LETTERS[1:3],nobs, T),
                  G2 = sample(LETTERS[1:3], nobs, T),
                  x=rnorm(nobs),
                  y=rnorm(nobs))
#introduce some missings in one group
dat$x[dat$G1=="C"] <- NA

#attempt to plot
p1 <- ggplot(dat, aes(x=x,y=y)) + facet_grid(G1~G2) + geom_point()
p1 #facets are generated according to the present levels of the grouping factors

enter image description here

#possible solution: remove the missing data before plotting
p2 <- ggplot(dat[complete.cases(dat),], aes(x=x, y=y)) + facet_grid(G1 ~G2) + geom_point()
p2

enter image description here

Heroka
  • 12,889
  • 1
  • 28
  • 38