Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

r - Center align table grob to x-axis label

I stole the example below (original found http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/#align-plot-panels). The output center aligns the table within the figure, but I want to center align the table to the x-axis label of the plot above. What is the easiest way to do this?

library("ggpubr")
library("grid")
library("gridExtra")
# Density plot of "Sepal.Length"
#::::::::::::::::::::::::::::::::::::::
density.p <- ggdensity(iris, x = "Sepal.Length", 
                       fill = "Species", palette = "jco")
# Draw the summary table of Sepal.Length
#::::::::::::::::::::::::::::::::::::::
# Compute descriptive statistics by groups
stable <- desc_statby(iris, measure.var = "Sepal.Length",
                      grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
# Summary table plot, medium orange theme
stable.p <- ggtexttable(stable, rows = NULL, 
                        theme = ttheme("mOrange"))
p <- arrangeGrob(density.p, stable.p, 
                 ncol = 1,
                 heights = c(2, 1))
grid.draw(p)

This is the output.

enter image description here

This is the output I would like (table shifted to right to align with x axis label). enter image description here

question from:https://stackoverflow.com/questions/65831927/center-align-table-grob-to-x-axis-label

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with patchwork:

library(patchwork)
#Code
G <- density.p/stable.p

Output:

enter image description here

Or this:

#Code 2
G <- density.p/(plot_spacer()+stable.p)

Output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...