将标题替换为ggplot中图形的底部

时间:2018-06-19 08:22:53

标签: r ggplot2 graph title

在我看来,这实际上是一个简单的调整,但我找不到一个简单的论据来做到这一点。我希望标题不在顶部,而是在图表的底部。我能用这个快速有效的任何文本行吗?

library(tidyverse)

set.seed(20)
df <- data.frame(expl = rbinom(n=100, size = 1, prob=0.08),
             resp = sample(50:100, size = 100, replace  = T))

df$expl <- factor(df$expl, levels = c("1", "0"))

ggplot(df, aes(x = resp, alpha = factor(expl, labels = c("Condition 1", "Condition 2")))) +
  geom_histogram(fill = "#bebada", bins = 10) +
  labs(title = "I want this title to the bottom of the graph") +
  labs(alpha = "Condition") +
  scale_alpha_manual(values = c(0.5, 1))

1 个答案:

答案 0 :(得分:0)

您可以使用库grid.arrange()中的gridExtra

p<-ggplot(df, aes(x = resp, alpha = factor(expl, labels = c("Condition 1", "Condition 2")))) +
  geom_histogram(fill = "#bebada", bins = 10) +
  labs(alpha = "Condition") +
  scale_alpha_manual(values = c(0.5, 1))

library(gridExtra)
grid.arrange(p, bottom="I want this title to the bottom of the graph")

enter image description here

如果您不想安装库,则只需添加前缀gridExtra::grid.arrange(p, bottom="I want this title to the bottom of the graph")