9

I would like to add text / title for my factors using ggplot2.

For exemple for data from {reshape2} library:

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

Factors labels are: Female and Male.

How can I add above them the title "sex"?

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
maycca
  • 3,848
  • 5
  • 36
  • 67
  • Well, you can pass `labeller = label_both` to `facet_grid`. It's not quite what you want (certainly not quite as...bold), but it's a start. – alistaire Mar 21 '16 at 01:02
  • Make another variable with sex as all its value and add this to facet before sex variable itself. – TheRimalaya Mar 21 '16 at 01:05
  • laziest option would be `gridExtra::grid.arrange(last_plot(), right="SEX ")`. A more thorough alternative would be to use `ggplotGrob`, `gtable_add_cols`, `gtable_add_grob` and `textGrob` – baptiste Mar 21 '16 at 01:07
  • Have a look on [this](http://stackoverflow.com/q/7603949/707145). – MYaseen208 Mar 21 '16 at 02:19
  • Thanks @alistaire I've tried this one but it just modify my previous labels to: Female -> sex:Female, Male -> sex:Male, doesn't create an "superior" sex name of variables... Maybe another suggestions? – maycca Mar 21 '16 at 03:50
  • great @baptiste, this works great ! it is not exactly what I wanted, but it satisfy all I was looking for ! ;) – maycca Mar 21 '16 at 03:52
  • thanks @MYaseen208 I didn't find this one ! :) – maycca Mar 21 '16 at 03:59
  • Or adapt [this](http://stackoverflow.com/questions/29311772/ggplot2-more-complex-faceting/29323739#29323739) – Sandy Muspratt Mar 21 '16 at 04:39

1 Answers1

6

Adapting this answer.

Slightly better version. Work out its own width.

library(reshape2)
library(ggplot2)
library(grid)
library(gtable)

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

# text, size, colour for added text
text = "SEX"
size = 30
col = "red"
face = "bold"

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the positions of the right strips in the layout: t = top, l = left, ...
strip <-c(subset(gt$layout, grepl("strip-r", gt$layout$name), select = t:r))

# Text grob
text.grob = textGrob(text, rot = -90,
   gp = gpar(fontsize = size, col = col, fontface = face))

# New column to the right of current strip
# Adjusts its width to text size
width = unit(2, "grobwidth", text.grob) + unit(1, "lines")
gt <- gtable_add_cols(gt, width, max(strip$r))  

# Add text grob to new column
gt <- gtable_add_grob(gt, text.grob, 
        t = min(strip$t), l = max(strip$r) + 1, b = max(strip$b))

# Draw it
grid.newpage()
grid.draw(gt)

Original

library(reshape2)
library(ggplot2)
library(grid)
library(gtable)

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

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the positions of the right strips in the layout: t = top, l = left, ...
strip <-c(subset(gt$layout, grepl("strip-r", gt$layout$name), select = t:r))

#  New column to the right of current strip
# Adjust the width to suit
gt <- gtable_add_cols(gt, unit(3, "lines"), max(strip$r))  

# Add text grob to new column; adjust cex (i.e., size) to suit
gt <- gtable_add_grob(gt, 
  textGrob("SEX", rot = -90, 
        gp = gpar(cex = 2, fontface = "bold", col = "RED")), 
        t = min(strip$t), l = max(strip$r) + 1, b = max(strip$b))

# Draw it
grid.newpage()
grid.draw(gt)

enter image description here

Community
  • 1
  • 1
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122