Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manipulate the strip text of facet_grid plots?

Tags:

r

ggplot2

I'm wondering how I can manipulate the size of strip text in facetted plots. My question is similar to a question on plot titles, but I'm specifically concerned with manipulating not the plot title but the text that appears in facet titles (strip_h).

As an example, consider the mpg dataset.

    library(ggplot2)      qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer) 

The resulting output produces some facet titles that don't fit in the strip.

I'm thinking there must be a way to use grid to deal with the strip text. But I'm still a novice and wasn't sure from the grid appendix in Hadley's book how, precisely, to do it. Also, I was afraid if I did it wrong it would break my washing machine, since I believe all technology is connected through The Force :-(

Many thanks in advance.

like image 832
briandk Avatar asked May 01 '10 18:05

briandk


People also ask

How do you remove facet labels?

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

How do you rename a facet grid label?

Change the text of facet labels Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

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.

How do I change the size of a facet label in ggplot2?

If we want to modify the font size of a ggplot2 facet grid, we can use a combination of the theme function and the strip. text. x argument. In the following R syntax, I'm increasing the text size to 30.


2 Answers

You can modify strip.text.x (or strip.text.y) using theme_text(), for instance

qplot(hwy, cty, data = mpg) +        facet_grid(. ~ manufacturer) +        opts(strip.text.x = theme_text(size = 8, colour = "red", angle = 90)) 

Update: for ggplot2 version > 0.9.1

qplot(hwy, cty, data = mpg) +        facet_grid(. ~ manufacturer) +        theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90)) 
like image 135
rcs Avatar answered Sep 22 '22 03:09

rcs


Nowadays the usage of opts and theme_text seems to be deprecated. R suggests to use theme and element_text. A solution to the answer can be found here: http://wiki.stdout.org/rcookbook/Graphs/Facets%20%28ggplot2%29/#modifying-facet-label-text

qplot(hwy, cty, data = mpg) +        facet_grid(. ~ manufacturer) +        theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90)) 
like image 37
moi Avatar answered Sep 24 '22 03:09

moi