Search code examples
rggplot2tibblefacet-wrap

Is there a way to omit variables with NA values from facet wrap plots?


# A tibble: 8 × 4
  measurement   log2_fc locus  operon
  <chr>           <dbl> <chr>  <chr> 
1 transcriptome     1   PA3552 arn   
2 transcriptome     1.5 PA3553 arn   
3 proteome         NA   PA3552 arn   
4 proteome          2   PA3553 arn   
5 transcriptome     2.5 PA1179 opr   
6 transcriptome     3   PA1180 opr   
7 proteome         NA   PA1179 opr   
8 proteome         NA   PA1180 opr

plot <- ggplot(data=x,aes(x=locus,y=log2_fc,color=measurement)) +
  geom_jitter()

plot + facet_wrap(~operon, ncol=2)

I'm working with the code above to create a plot comparing log2_fc of genes as obtained through two different measurement methods. I want to separate the plot out by the operon that the genes belong to but I would like to only have the genes in that operon plotted along the x axis in each facet. Currently it is creating the plot below:

facet_wrap plot of tibble x

Is there a way to only plot each locus value once along the x axis and still have the data separated by operon?


Solution

  • You just need to free the x axis:

    plot + facet_wrap(~operon, ncol=2, scales = "free_x")
    

    Without specifying scales = "free_x", ggplot defaults to identical axes with the same limits and breaks.