如何在ggplot中空白轴标题而不影响绘图大小? [英] How do I blank axis titles in ggplot without affecting the plot size?
问题描述
我想一起显示相关的ggplots,其轴的大小均相同,但是有些图显示了轴标题,而另一些图则使轴标题空白.但是,使用element_blank()删除轴也会更改绘图的大小.这是一个示例:
I want to display related ggplots together, with axes all the same size, but with some plots displaying the axis title and other plots blanking the axis title. However, using element_blank() to remove the axis also changes the size of the plot. Here's an example:
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轴将展开.有办法防止这种情况发生吗?
Here, the y-axis of the plot without an x-axis title is expanded. Is there a way to prevent this happening?
我知道我可以通过更改ggdraw调用中的高度和位置来伪造一个修复程序,但是我想要一个适用于ggplots的修复程序.我也想像这是一个普遍的问题,但是还没有找到解决方案.
I'm aware I can fudge a fix by changing the height and position in the call to ggdraw, but I'd like a fix that works on the ggplots. I also imagine this is a common problem, but haven't been able to find a solution out there.
推荐答案
将x轴的名称指定为空字符串,即"
而不是 element_blank()
Specify the name of the x-axis as empty string, i.e. ""
instead of element_blank()
...
without_x_title <- ggplot(tb, aes(a,b)) +
geom_point() +
# theme(axis.title.x = element_blank()) +
labs(x = "")
...
这篇关于如何在ggplot中空白轴标题而不影响绘图大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!