How can I increase the area around a plot area in ggplot 2 to give my axis titles some breathing room. I am aware of vjust and hjust (as below), however, I can't seem to create actual space around the plotting area to move my axes titles onto.
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p
p<- p + theme(axis.title.x = element_text(family="Times",size=20,face="bold",colour = "Black",vjust=-1,hjust=0.5))
p
Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.
Increase margins In order to modify the plot margins set the margin function inside the plot. margin component of the theme function. The margins are measured with points ( "pt" ), but you can use other unit measure in the unit argument, like centimeters.
ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.
ggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 . Notice how ggplot is able to use either numerical or categorical (factor) data as x and y coordinates.
Margins around plot can be modified with theme()
, plot.margin =
and function margin()
where you provide size of margins starting with top, then right, bottom and left, and units (default is "pt").
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
theme(axis.title.x = element_text(family = "Times",size = 20,
face = "bold",colour = "Black",vjust = -1,hjust = 0.5))+
theme(plot.margin = margin(1,1,1.5,1.2, "cm"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With