如果我正确理解了这个问题,一种快速的管理方法是使用 gridextra 和 grid,因为它允许您与图形对象进行交互。下面是我的代码一步一步:
library(ggplot2)
library(FactoMineR)
library(factoextra)
#load gridExtra
library(gridExtra)
#save as an object within global environment
irispca <- PCA(iris,quali.sup = 5)
plots<- fviz_pca_var(irispca)+
theme(text = element_text(size = 7.5),
axis.title = element_text(size = 7.5),
axis.text = element_text(size = 7.5))
)
#use grid.arrange from gridextra to adjust the widths and heights
# to your liking. You can do this multiple plots if you wanted to
#but grid.arrange offers a quick workaround
grid.arrange(plots,widths = unit(6,"cm"))
如果您想深入研究 ggplot_grob 并获得更具体的内容,您可以使用 ggplot_grob 编辑 ggplot 对象的 gtable。使用它,您可以直接操作 gtable 参数。您可以使用以下操作并检查您需要的 gtable 参数。
library(ggplot2)
library(FactoMineR)
library(factoextra)
library(gridExtra)
irispca <- PCA(iris,quali.sup = 5)
plots<- fviz_pca_var(irispca)+
theme(text = element_text(size = 7.5),
axis.title = element_text(size = 7.5),
axis.text = element_text(size = 7.5))
)
library(gtable)
plottable<- ggplotGrob(plots)
plottable$widths[4:6] <- unit(3, "cm")
grid.newpage()
gtable_show_layout(plottable)
grid.draw(plottable)
`
您可以进行相应调整,但这里是使用上述代码和特征显示绘图布局的图像。 plottable$layout 也将有助于理解 gtable 对象中被绘制的每个区域。
编辑:针对下面的评论,这里有进一步的规范,将 x 轴更改为 4 厘米,将绘图本身更改为 6。只要 grid.draw 和 gtable_show_layout 工作,grid.newpage 并不重要。
library(ggplot2)
library(FactoMineR)
library(factoextra)
library(gridExtra)
library(gtable)
library(grid)
irispca <- PCA(iris,quali.sup = 5)
plots<- fviz_pca_var(irispca)+
theme(text = element_text(size = 7.5),
axis.title = element_text(size = 7.5),
axis.text = element_text(size = 7.5))
)
plottable<- ggplotGrob(plots)
plottable$widths[5] <- unit(4, "cm")#controls x-axis
plottable$widths[c(4,6)] <- unit(1,"cm")#controls margins
grid.newpage()
gtable_show_layout(plottable)
grid.draw(plottable)#to just get the plot, do not use the gtable_show_layout
plot(plottable) #if grid.draw does not work, just use the plot function and it should plot it correctly. Note: this will only work if the widths were adjusted correctly in plottable.