Search code examples
rplotprintingformattingcowplot

Format ggplot font sizes for printing on paper


My goal is to generate plot in R so that it would print nicely on letter paper. Below is my example:

library(cowplot)

sample_df <- data.frame(col_1=c(1,2,3), col_2=c(6,7,8))
plot_1 <-ggplot(data=sample_df, aes(x = col_1, y =col_2, group=1))+
  geom_line()+ggtitle('Title 1')
plot_2 <-ggplot(data=sample_df, aes(x = col_1, y =col_2, group=1))+
  geom_line()+ggtitle('Title 2')

width_letter = 6
height_letter = width_letter*8.5/11

pdf('outpdf_1.pdf', width=width_letter, height=height_letter)
plot_grid(plot_1, plot_2, labels = "AUTO", ncol = 2, align = 'v', scale = 0.65) +   
  theme(plot.margin = unit(c(2,2,2,2), "cm")) 

dev.off()

It generates ugly plot, I seem not be able to find a way to format plot nicely i.e. resize axis and fonts. Any suggestions?Image


Solution

  • Right after library(cowplot) run the following code: theme_set(theme_cowplot(font_size=8)). The default font size for theme_cowplot() (the ggplot theme that becomes the default when you load the cowplot package) is 14. Changing this to a lower value will reduce the font sizes in your plots. You can also change individual font sizes for various plot elements (e.g., axis labels, plot title, etc.) with theme statements when you create each plot.

    Here's what the PDF file looks like with the smaller font size:

    enter image description here

    As another option, when you open the PDF device, you can use a multiple of the height and width values you created. For example: pdf('outpdf_1.pdf', width=width_letter*2, height=height_letter*2). This will result in text that is smaller, relative to the plot area, but the plot will still be rendered within a single page in the PDF file. I prefer the first method, but you can go with this approach also.

    To respond to your comment: ggplot's default theme is theme_grey (run ?theme_grey to see the setting for this and other available themes). You can see that each theme has a base_size with a default setting. All the of the font sizes for labels and titles are scaled relative to the base_size. You can see all the various setting for each theme by typing its name in the console. For example, run theme_grey.

    When I first started using cowplot, I noticed that when you load it, it defaults to a new theme, theme_cowplot. Type ?theme_cowplot to see its help file and theme_cowplot to see all the settings. You'll notice that unlike the default ggplot themes, theme_cowplot creates a new argument, font_size, which plays the same role as base_size in ggplot's default themes. Looking over the output of theme_cowplot you can see that it starts with theme_grey(base_size=font_size, ...).

    More generally, you can always start with some theme, cowplot, grey, classic, etc., modify the overall font scaling with base_size (or font_size in theme_cowplot) and then change individual fonts with a theme statement. You can also store these changes in a new theme object that you can then apply to any plot, so that you don't have to repeat all the theme elements with each new plot. There's also a ggthemes package that has additional pre-packaged themes.