当我向期刊提交论文时,期刊要求我将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)
)
答案 0 :(得分:0)
如果我正确理解了这个问题,一个快速管理方法就是使用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.newpage只要grid.draw和gtable_show_layout工作就不那么重要了。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.