Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove empty factors from ggplot2 facets?

Tags:

r

ggplot2

I'm trying to modify an example of a simple forest plot by introducing facets according to a factor variable.

Assuming data of this structure:

test <- structure(list(characteristic = structure(c(1L, 2L, 3L, 1L, 2L ), .Label = c("Factor1", "Factor2", "Factor3"), class = "factor"),      es = c(1.2, 1.4, 1.6, 1.3, 1.5), ci_low = c(1.1, 1.3, 1.5,      1.2, 1.4), ci_upp = c(1.3, 1.5, 1.7, 1.4, 1.6), label = structure(c(1L,      3L, 5L, 2L, 4L), .Label = c("1.2 (1.1, 1.3)", "1.3 (1.2, 1.4)",      "1.4 (1.3, 1.5)", "1.5 (1.4, 1.6)", "1.6 (1.5, 1.7)"), class = "factor"),      set = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("H", "S"     ), class = "factor")), .Names = c("characteristic", "es",  "ci_low", "ci_upp", "label", "set"), class = "data.frame", row.names = c(NA,  -5L)) 

And running the code:

p <- ggplot(test, aes(x=characteristic, y=es, ymin=ci_low, ymax=ci_upp)) + geom_pointrange() +   coord_flip() + geom_hline(aes(x=0), lty=2) +    facet_wrap(~ set, ncol = 1) +   theme_bw() +    opts(strip.text.x = theme_text()) 

Produces output like that:

enter image description here

All good so far. However, I'd like to get rid of empty Factor3 level from my lower panel and cannot find a way to do that. Is there any way to do that?

Thanks for help.

like image 736
radek Avatar asked Apr 16 '12 23:04

radek


People also ask

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

How do you remove a facet title?

Facet labels text element in theme() to element_blank() . Setting strip. text to element_blank() will remove all facet labels. You can also remove the labels across rows only with strip.

What is facet wrap in ggplot2?

17.1 Facet wrap This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as. table and dir . ncol and nrow control how many columns and rows (you only need to set one).


1 Answers

EDIT Updated to ggplot2 0.9.3

Here's another solution. It uses facet_grid and space = "free"; also it uses geom_point() and geom_errorbarh(), and thus there is no need for coord.flip(). Also, the x-axis tick mark labels appear on the lower panel only. In the code below, the theme command is not essential - it is used to rotate the strip text to appear horizontally. Using the test dataframe from above, the following code should produce what you want:

library(ggplot2)  p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) +    geom_point() +    geom_errorbarh(height = 0) +    facet_grid(set ~ ., scales = "free", space = "free") +    theme_bw() +    theme(strip.text.y = element_text(angle = 0))  p 

The solution is based on the example on page 124 in Wickham's ggplot2 book.

like image 97
Sandy Muspratt Avatar answered Oct 03 '22 04:10

Sandy Muspratt