gpt4 book ai didi

r - 如何删除 ggplot 中的刻度标签而不影响绘图大小?

转载 作者:行者123 更新时间:2023-12-02 03:21:19 28 4
gpt4 key购买 nike

我想一起显示相关的ggplots,轴的大小相同,但一些图显示轴刻度标签,而其他图则空白轴刻度标签。下面的代码显示了我删除刻度标签的两种方法,但都调整了绘图的大小:

library(tidyverse)
library(cowplot)

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

without_x_title <- ggplot(tb, aes(a,b)) +
geom_point() +
labs(x = "")

without_x_title_labels_1 <- ggplot(tb, aes(a,b)) +
geom_point() +
labs(x = "") +
scale_x_discrete(labels = "")

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

ggdraw() +
draw_plot(without_x_title, x = 0, y = 0, width = 0.3, height = 1) +
draw_plot(without_x_title_labels_1, x = 0.3, y = 0, width = 0.3, height = 1) +
draw_plot(without_x_title_labels_2, x = 0.6, y = 0, width = 0.3, height = 1)

有办法避免这种情况发生吗?

这是关于删除轴标签的类似问题的后续问题 here 。我知道我可以通过更改 ggdraw 调用中的高度和位置来伪造修复程序,但我想要一个适用于 ggplots 的修复程序。与上一个问题一样,我认为这是一个常见问题,但尚未找到解决方案。

最佳答案

为什么不使用plot_grid函数?

library(tidyverse)
library(cowplot)

plot_grid(without_x_title, without_x_title_labels_1, without_x_title_labels_2,
ncol = 3,
align = 'hv',
axis = 'tb',
labels = "AUTO")

如果您使用 patchwork 包并想要绘图注释:

library(patchwork)
without_x_title + without_x_title_labels_1 + without_x_title_labels_2 +
plot_annotation(tag_levels = 'A',
tag_suffix = ')')

reprex package于2019年2月13日创建(v0.2.1.9000)

关于r - 如何删除 ggplot 中的刻度标签而不影响绘图大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54679397/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com