0

I am faceting a bar plot in ggplot2 but I want to remove the IDs with no y variable associated with them in a particular group. An example with representative data set (given below) is

    ID VAL GRP
1   1   2   1
2   2   4   2
3   3   6   3
4   4   8   4
5   5  10   1
6   6  12   2
7   7  14   3
8   8  16   4
9   9  18   1
10 10  20   2
11 11  22   3
12 12  24   4

Here is the complete code

    df <- structure(list(ID = 1:12, VAL = c(2, 4, 6, 8, 10, 12, 14, 16, 
    18, 20, 22, 24), GRP = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L)), .Names = c("ID", "VAL", "GRP"), row.names = c(NA, 
    -12L), class = "data.frame")

    ggplot(df, aes(x = as.factor(ID), y = VAL)) + 
      geom_bar(aes(fill = as.factor(GRP)), stat = "identity") + 
      facet_wrap(~as.factor(GRP))

Plot generated is given below, however I want a plot where each facet does NOT have the IDs not belonging to that particular group as the ordering of IDs does not mean anything, they are just identifiers.

Thanks!

enter image description here

Satya
  • 1,708
  • 1
  • 15
  • 39

1 Answers1

1

Add scales="free_x" to your facet_wrap call.

iod
  • 7,412
  • 2
  • 17
  • 36