Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing area around plot area in ggplot2 [duplicate]

Tags:

plot

r

ggplot2

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

enter image description here

like image 220
Elizabeth Avatar asked Aug 15 '13 12:08

Elizabeth


People also ask

How do you expand plot area in R?

Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.

How do I change the margins in ggplot2?

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.

What is GG in Ggplot?

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.

Does Ggplot only work with data frames?

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.


1 Answers

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"))
like image 91
Didzis Elferts Avatar answered Oct 18 '22 03:10

Didzis Elferts