ckluss
ckluss

Reputation: 1271

ggplot2: change strip.text position in facet_grid plot

you can set the position of the legend inside the plotting area, like

... + theme(legend.justification=c(1,0), legend.position=c(1,0))

Is there a similarly easy way to change the position of the strip text (or factor levels in grouped plots)

library(reshape2); library(ggplot2)

sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point() +
  facet_grid(. ~ sex)

sp

enter image description here

(http://www.cookbook-r.com/Graphs/Facets_%28ggplot2%29/)

in lattice I would use something like strip.text = levels(dat$Y)[panel.number()] and panel.text(...), but there may be a cleaner way too...

thx, Christof

Upvotes: 13

Views: 7041

Answers (2)

L-P Dagallier
L-P Dagallier

Reputation: 11

A slight addition to @JasonAizkalns is to add the check_overlap = T option in geom_text, to avoid the superposition of multiple identical labels.

  ggplot(tips, aes(x = total_bill, y = tip / total_bill)) +
  geom_point() +
  facet_grid(. ~ sex) +
  geom_text(aes(label = sex), x = Inf, y = Inf, hjust = 1.5, vjust = 1.5, check_overlap = TRUE) +
  theme(
      strip.background = element_blank(),
            strip.text = element_blank()
  )

Upvotes: 1

JasonAizkalns
JasonAizkalns

Reputation: 20463

Here's one approach:

ggplot(tips, aes(x = total_bill, y = tip / total_bill)) +
  geom_point() +
  facet_grid(. ~ sex) +
  geom_text(aes(label = sex), x = Inf, y = Inf, hjust = 1.5, vjust = 1.5) +
  theme(
      strip.background = element_blank(),
            strip.text = element_blank()
  )

However, this is not moving the strip.text, rather, it's adding a geom_text element and turning off the strip.background and strip.text, but I think it achieves the desired outcome.

Plot

Upvotes: 14

Related Questions