r - 如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在一系列绘图中创建 X 和 Y 轴的公共标题?
问题描述
我有一组图,我想为其创建一个常见的 X 轴标题和一个常见的 Y 轴标题。举个例子:
library(ggplot2)
library(cowplot)
library(ggpubr)z
theme_set(theme_cowplot())
df <- data.frame(x = 1:12, y = (1:12)^2)
df$grp = c('Some', 'Things', 'In a legend')
p <- ggplot(df, aes(x, y, color=grp)) + geom_point()
leg <- get_legend(p)
leg <- as_ggplot(leg)
p = p + theme(legend.position = "none")
plot_grid(
p, p, leg,
p, p, leg,
ncol = 3, rel_widths=c(2,2,1)
)
经过一番阅读,我发现可以选择使用ggdraw
. 但是,我不知道它是如何运作的,我也不知道如何得到我想要的(一个Y
在 y 轴,一个X
在 x 轴)。以下是我尝试过的:
- 我做的和以前一样,但我也删除了每个图的 X 和 Y 轴标题
df <- data.frame(x = 1:12, y = (1:12)^2)
df$grp = c('Some', 'Things', 'In a legend')
p <- ggplot(df, aes(x, y, color=grp)) + geom_point()
leg <- get_legend(p)
leg <- as_ggplot(leg)
p = p + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank()) # I remove x and y titles for the plots
P <-plot_grid(
p, p, leg,
p, p, leg,
ncol = 3, rel_widths=c(2,2,1)
)
P
- 然后,使用
ggdraw
,我在我想要的位置创建了一组带有一个 x 轴标题的图。但是,我不知道如何为 y 轴获得相同的值。我不知道如何增加 y 轴边距以添加标签。以下是我到目前为止尝试的代码:
P1 <- ggdraw(add_sub(P , "X", vpadding=grid::unit(0.8,"lines"), # With `0.8` I add some space in the X margin.
y=2, x=1, vjust=1, # With `y` and `x` I can indicate the coordinates for my desired text
angle=0) # With this I can change the orientation of the text
)
P1
有谁知道如何使用ggdraw
或与另一个函数一起创建一个通用的 x 和 y 轴?
解决方案
推荐阅读
- python - 是否可以在基于函数的视图中覆盖 LoginView,而不是作为一个类?
- angular - Angular &TypeScript Intellisense 在 *ngFor 语句中不起作用
- php - 如何从子目录重定向到子域
- python - 使用 for 循环遍历 DataFrame 对象列表
- php - 如何从 foreach 内的查询中传递数组
- javascript - 手动将 es6 箭头函数转换为 es5?
- vb.net - 如何检索 Symbol 移动设备的 OEM 名称?
- java - 对话框后刷新 Listview
- hadoop - java.lang.IllegalArgumentException:错误的文件系统
- markdown - Markdown 中的研究论文摘要段落