Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change strip.text labels in ggplot with facet and margin=TRUE

I have looked here but still can't figure it out. How do I change the strip.text.x labels in a ggplot with faceting? Specifically I am using facet_grid with margins. The strip.text label for the margin is "(all)" - but since I am in a non-english speaking country I would rather write "Total" or something similar in my native tongue.

opts(stip.text.x=c(levels(facetvariabel,"Total")) does not work.

Any ideas?

Example (not really the best dataset for this - but I guess it will work)

ggplot(cars, aes(x=dist))+geom_bar()+facet_grid(.~speed, margin=T)
like image 574
Andreas Avatar asked May 23 '10 18:05

Andreas


People also ask

How do you change facet wrap labels in R?

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.

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

By default, the size of the label is given by the Facets, here it is 9. But we can change the size. For that, we use theme() function, which is used to customize the appearance of plot. We can change size of facet labels, using strip.

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_grid and Facet_wrap?

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.


1 Answers

You can customize the facet labels by giving labeller function:

f <- function(x, y) {
  if (x == "speed")
    c(y[-length(y)], "Total")
  else
    y
}

ggplot(cars, aes(x = dist)) +
  geom_bar() +
  facet_grid(. ~ speed, margin = TRUE, labeller = f)
like image 103
kohske Avatar answered Sep 17 '22 20:09

kohske