Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The size of a ggplot2 plot can be adjusted by changing the plot dimensions or using the theme() function to adjust the plot margins. However, this can also alter the size of the axis labels. To adjust the plot size without altering the size of the axis labels, you can use the axis.text argument within the element_text() function to explicitly set the font size of the axis labels.

For example, to create a ggplot2 plot with a specific plot size and axis label font size, you can use the following code:

library(ggplot2) # create a sample plot p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() # set plot dimensions and margin size p + theme( plot.background = element_rect(fill = "white"), plot.margin = unit(c(1, 1, 2, 2), "cm") ) + # adjust the font size of the axis labels theme( axis.text = element_text(size = 12) ) 

In this example, the plot.margin argument within the theme() function adjusts the plot dimensions and margin size. The axis.text argument within the second theme() function sets the font size of the axis labels to 12 points, without altering the plot size or the rest of the theme.