Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression in ggplot2 facet labels

I would like to have an R expression in a ggplot2 facet label.

Let's say I'm plotting the tips data.frame:

library(reshape2)
> head(tips)
  total_bill  tip    sex smoker day   time size
1      16.99 1.01 Female     No Sun Dinner    2
2      10.34 1.66   Male     No Sun Dinner    3
3      21.01 3.50   Male     No Sun Dinner    3
4      23.68 3.31   Male     No Sun Dinner    2
5      24.59 3.61 Female     No Sun Dinner    4
6      25.29 4.71   Male     No Sun Dinner    4

as follows:

library(ggplot2)
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
         geom_point(shape=1) + 
         facet_wrap(~sex, ncol = 1)

enter image description here

Instead of having "Female" and "Male" as facet labels I would like to have: "Female subjects" and "Male subjects", respectively. As far as I know italicizing a label in R is achieved through the expression function, but I don't know how to combine that with facet_wrap.

like image 994
user1701545 Avatar asked Sep 19 '15 17:09

user1701545


3 Answers

The following works for R 4.0.3 and ggplot2 3.3.2

library(ggplot2)
data(tips, package="reshape2")
tips$sex = as.character(tips$sex)
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  facet_wrap(~sex, ncol = 1, labeller = label_bquote(italic(.(sex))~subjects))
sp

It uses label_bquote as suggested by @Stéphane Laurent but does not require converting levels into the correct character within the bquote which failed for me.

like image 178
jakob-r Avatar answered Oct 24 '22 05:10

jakob-r


As of ggplot2 2.1.0:

data(tips, package="reshape2")
library(ggplot2)
ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  facet_wrap(~sex, ncol=1, 
             labeller=label_bquote(.(levels(tips$sex)[sex])~subjects))
like image 41
Stéphane Laurent Avatar answered Oct 24 '22 04:10

Stéphane Laurent


Looks overly complicated, but works. You'll have to use facet_grid though.

make_label <- function(value) {
  x <- as.character(value)
  bquote(italic(.(x))~subjects)
}

plot_labeller <- function(variable, value) {
  do.call(expression, lapply(levels(value), make_label))
}

ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  facet_grid(.~sex, labeller = plot_labeller)

enter image description here

like image 25
tonytonov Avatar answered Oct 24 '22 05:10

tonytonov