ggplot2:在facet_grid图中更改strip.text位置

时间:2016-03-18 16:50:26

标签: r plot ggplot2

您可以在绘图区域内设置图例的位置,例如

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

是否有类似的简单方法来更改条带文本的位置 (或分组图中的因子水平)

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/

格子中的

我会使用像strip.text = levels(dat $ Y)[panel.number()]这样的东西 和panel.text(...),但也可能有一个更清洁的方式...

thx,Christof

1 个答案:

答案 0 :(得分:10)

这是一种方法:

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()
  )

但是,这不会移动strip.text,而是添加geom_text元素并关闭strip.backgroundstrip.text,但我认为它达到了预期结果

Plot