简体   繁体   English

如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在 X 和 Y 轴上创建一个公共标题?

[英]How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R?

I have an arrange of plots for which I would like to create a common X-axe title and a common Y-axe title.我有一组图,我想为其创建一个常见的 X 轴标题和一个常见的 Y 轴标题。 As an example:举个例子:

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)
)

在此处输入图像描述

After some reading, I found as an option the use of ggdraw .经过一番阅读,我发现可以选择使用ggdraw However, I don't know how it works well, and I don't figure out how to get what I want (one Y in the y-axis and one X in the x-axis).但是,我不知道它是如何工作的,也不知道如何得到我想要的(y 轴上的一个Y和 x 轴上的一个X )。 Below is what I tried:以下是我尝试过的:

  • I did the same than before but I removed also X and Y-axis titles for each plot我做的和以前一样,但我也删除了每个 plot 的 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
  • Then, using ggdraw , I created an arrange of plots with one x-axis title in my desired position.然后,使用ggdraw ,我在我想要的 position 中创建了一系列带有一个 x 轴标题的图。 However, I don't know how to get the same for the y-axe.但是,我不知道如何为 y 轴获得相同的值。 I don't know how to increase the y-axe margin to add a label.我不知道如何增加 y 轴边距以添加 label。 Below is the code I tried so far:以下是我到目前为止尝试的代码:
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 

在此处输入图像描述

Does anyone know how to create a common x and y-axis using ggdraw or with another function?有谁知道如何使用ggdraw或另一个 function 创建一个通用的 x 和 y 轴?

I think you just need to play around with the margins before add_sub :我认为您只需要在add_sub之前使用边距:

P <-plot_grid(
 p, p, leg, 
 p, p, leg,
 ncol = 3, rel_widths=c(2,2,1)
) + theme(plot.margin = margin(30, 30, -50, 50))

P <- add_sub(P, "x axis text", hjust = 1)
P <- add_sub(P, "y axis text", -0.05, 5, angle = 90)
ggdraw(P)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? 如何使用 R 中的 plot_grid 函数在图之间将 y 轴对齐 0? - How to align y axis by 0 between plots with plot_grid function in R? 在 r 中,如何控制组合图 (plot_grid) 中图与 x 轴的比例? - In r, how to control proportion of plots versus x-axis in a combined plot (plot_grid)? 如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例? - How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package? 使用 plot_grid 排列图,其中 plot 信息存储在 R 数据框中 - Use plot_grid to arrange plots where plot information is stored in an R data frame 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language 两个图相同的x轴,两个y轴-如何在网格中排列图 - Two plots same x axis, two y axis - how to arrange plots in a grid 如何使用plot_grid放置没有任何空间的绘图? - How to put plots without any space using plot_grid? 如何添加一个常见的 y 和 x label 来排列地块,并为 R 中的几列添加 label? - How to add one common y and x label for an arrange of plots and also a label for several columns of this arrange in R? R Plot 条在 grid.arrange/plot_grid 中消失 - R Plot Bars are disappearing in grid.arrange/plot_grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM