首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置factoextra或ggplot2中x轴的总长度

如何设置factoextra或ggplot2中x轴的总长度
EN

Stack Overflow用户
提问于 2018-05-19 08:20:38
回答 1查看 539关注 0票数 1

当我向一家期刊提交论文时,期刊要求我将x轴的总长度设置为4厘米,但我找不到一个增量来设置轴的总长度,我不是指图片的宽度。

下面是一些示例代码:

代码语言:javascript
复制
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)
     )

EN

回答 1

Stack Overflow用户

发布于 2018-05-19 13:55:49

如果我对这个问题的理解是正确的,那么解决这个问题的一个快速方法是使用gridextra和grid,因为它允许您与图形对象进行交互。下面是我一步一步的代码:

代码语言:javascript
复制
   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参数,directly.You可以使用以下命令来操作,并检查您需要的gtable参数。

代码语言:javascript
复制
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 cm,将绘图本身更改为6。只要grid.draw和gtable_show_layout工作,grid.newpage就不那么重要。

代码语言:javascript
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50420660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档