当我向一家期刊提交论文时,期刊要求我将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)
)

发布于 2018-05-19 13:55:49
如果我对这个问题的理解是正确的,那么解决这个问题的一个快速方法是使用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参数,directly.You可以使用以下命令来操作,并检查您需要的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 cm,将绘图本身更改为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.https://stackoverflow.com/questions/50420660
复制相似问题