【问题标题】:How do I blank axis titles in ggplot without affecting the plot size?如何在 ggplot 中空白轴标题而不影响绘图大小?
【发布时间】:2019-02-13 19:56:16
【问题描述】:

我想一起显示相关的 ggplots,轴的大小都相同,但有些图显示轴标题,而其他图则空白轴标题。但是,使用 element_blank() 删除轴也会改变绘图的大小。这是一个例子:

library(tidyverse)
library(cowplot)

tb <- tibble(a = 1:5, b = 1:5)

with_x_title <- ggplot(tb, aes(a,b)) +
  geom_point()

without_x_title <- ggplot(tb, aes(a,b)) +
  geom_point() +
  theme(axis.title.x = element_blank())

ggdraw() +
  draw_plot(with_x_title, x = 0, y = 0, width = 0.5, height = 1) +
  draw_plot(without_x_title, x = 0.5, y = 0, width = 0.5, height = 1)

在这里,没有 x 轴标题的绘图的 y 轴被展开。有没有办法防止这种情况发生?

我知道我可以通过更改对 ggdraw 的调用中的高度和位置来伪造修复,但我想要一个适用于 ggplots 的修复。我也认为这是一个常见问题,但一直无法找到解决方案。

【问题讨论】:

  • 如果你不是theme(axis.title.x = element_blank()),而是xlab(" ")。只需设置一个空格作为轴标签。

标签: r ggplot2


【解决方案1】:

将x轴的名称指定为空字符串,即""而不是element_blank()

...

without_x_title <- ggplot(tb, aes(a,b)) +
  geom_point() +
  # theme(axis.title.x = element_blank()) +
  labs(x = "")

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多