1

When I submitted a paper to a journal, the journal asked me to set the total length of x axis to 4 cm, however I can not find an augment to set the total length of a axis, I don't mean the width of the picture.

Some example codes:

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)
     )

required length of x

Minyi Han
  • 807
  • 1
  • 8
  • 15
  • Not sure I get what you are trying to achieve but you could try adding +coord_equal(expand=F). From the documentation, this way you ensure that: "the limits are taken exactly from the data or xlim/ylim" – Julien Massardier May 19 '18 at 00:37
  • thanks, I want to control the width of x axis to a specific value. – Minyi Han May 19 '18 at 01:47
  • Are they asking this for printing purposes? Maybe they don't need it to be exactly 4 cm, and roughly would be ok? My guess is that, because the x length will always be proportional to the size of the figure, you need to figure out what figure size returns an x axis length of 4cm. – Julien Massardier May 19 '18 at 02:10
  • Maybe, thanks for your useful suggestion – Minyi Han May 19 '18 at 03:05
  • Check out the highest voted answer (not the accepted answer) in the link to [specify width and height of plot](https://stackoverflow.com/questions/1279003/specify-width-and-height-of-plot) – steveb May 19 '18 at 06:04
  • A thought and question, have you asked for advice from the journal on how to do this ? Perhaps they know how others are doing it. What format should it be in (e.g. pdf, jpeg, png,...) ? – steveb May 19 '18 at 06:07

1 Answers1

0

If I'm understanding the question correctly, one quick way to manage this is to use gridextra and grid since it allows you to interact with graphical objects. Below is my code step by step:

   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"))

If you want to delve into ggplot_grob and the get more specific, you can edit the gtable of the ggplot object using ggplot_grob. Using this you can manipulate the gtable arguments directly.You can do so with the following and inspect the gtable arguments to you need.

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)

`

You can adjust accordingly but here is an image of how the plot layout looks with the above code along with the characteristics. plottable$layout will also help understand each of the zones within the gtable object that gets plotted.

EDIT: In response to the comment below, here is further specification to change the x-axis to 4 cm and the plot itself to 6. grid.newpage isn't as important as long as grid.draw and gtable_show_layout work.

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.
  • When I performed the code, it prompted me: Error in grid.newpage() : could not find function "grid.newpage" – Minyi Han May 19 '18 at 08:37
  • The above codes control the width of the plot, and I also want to control the width of x axis. – Minyi Han May 19 '18 at 08:41
  • Edited the code above to account for the specifications. Let me know if that worked for you @MinyiHan – Sebastian Hoyos May 19 '18 at 18:01
  • How can I obtain the final plot, @Sebastian Hoyos run grid.draw(plottable) or plottable is not working – Minyi Han May 28 '18 at 07:28
  • if grid.draw isn't working, you can also just use plot(plottable) after you've made the edits to the gtable . The only issue with plot(plottable) is that it will show a grid. I've edited the code above to include the plot option as well. This should work as long as thhe gtable within plottable is edited for the widths – Sebastian Hoyos May 28 '18 at 21:02