【问题标题】:How to set the total length of x axis in factoextra or ggplot2如何在factoextra或ggplot2中设置x轴的总长度
【发布时间】:2025-12-03 10:30:01
【问题描述】:

当我向期刊投稿时,期刊要求我将 x 轴的总长度设置为 4 厘米,但是我找不到设置轴总长度的增强器,我不是说图片的宽度。

一些示例代码:

library(ggplot2)
library(FactoMineR)
library(factoextra)

irispca <- PCA(iris,quali.sup = 5)
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)
     )

【问题讨论】:

  • 不确定我是否得到您想要实现的目标,但您可以尝试添加 +coord_equal(expand=F)。从文档中,您可以确保:“限制完全取自数据或 xlim/ylim”
  • 谢谢,我想把x轴的宽度控制到一个特定的值。
  • 他们问这个是为了打印吗?也许他们不需要它正好是 4 厘米,大致可以吗?我的猜测是,因为 x 长度总是与图形的大小成正比,所以你需要弄清楚什么图形大小会返回 4cm 的 x 轴长度。
  • 也许吧,谢谢你的有用建议
  • 查看specify width and height of plot链接中投票最高的答案(不是接受的答案)

标签: r ggplot2


【解决方案1】:

如果我正确理解了这个问题,一种快速的管理方法是使用 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.

【讨论】:

  • 当我执行代码时,它提示我:Grid.newpage() 中的错误:找不到函数“grid.newpage”
  • 以上代码控制了绘图的宽度,我也想控制x轴的宽度。
  • 编辑了上面的代码以说明规范。让我知道这是否对你有用@MinyiHan
  • 我怎样才能获得最终的情节,@Sebastian Hoyos 运行 grid.draw(plottable) 或 plottable 不起作用
  • 如果 grid.draw 不起作用,您也可以在对 gtable 进行编辑后使用 plot(plottable) 。 plot(plottable) 的唯一问题是它会显示一个网格。我已经编辑了上面的代码以包含绘图选项。只要在 plottable 中为宽度编辑 gtable,这应该可以工作
最近更新 更多