Avoid overlapping axis labels in R
Use par(mar)
to increase the plot margins and par(mgp)
to move the axis label.
par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))#Then call plot as before
In the help page ?par
it explains which parameters can be used directly in plot
and which must be called via par
.
There are several parameters can only be set by a call to ‘par()’:
• ‘"ask"’, • ‘"fig"’, ‘"fin"’, • ‘"lheight"’, • ‘"mai"’, ‘"mar"’, ‘"mex"’, ‘"mfcol"’, ‘"mfrow"’, ‘"mfg"’, • ‘"new"’, • ‘"oma"’, ‘"omd"’, ‘"omi"’, • ‘"pin"’, ‘"plt"’, ‘"ps"’, ‘"pty"’, • ‘"usr"’, • ‘"xlog"’, ‘"ylog"’ The remaining parameters can also be set as arguments (often via ‘...’) to high-level plot functions such as ‘plot.default’, ‘plot.window’, ‘points’, ‘lines’, ‘abline’, ‘axis’, ‘title’, ‘text’, ‘mtext’, ‘segments’, ‘symbols’, ‘arrows’, ‘polygon’, ‘rect’, ‘box’, ‘contour’, ‘filled.contour’ and ‘image’. Such settings will be active during the execution of the function, only. However, see the comments on ‘bg’ and ‘cex’, which may be taken as _arguments_ to certain plot functions rather than as graphical parameters.
The quick and dirty way would be to use par
and add a newline in ylab
, even though it's conceptually terrible.
x = 0:10y = sin(x) + 10par(mar=c(5,7,4,2))plot ( x, y, type="o", xlab = "X values", ylab = "Y values\n", cex.axis = "2", cex.lab = "2", las = 1)
Concerning which parameters you can set directly in plot
have a look at ?plot.default
and ?plot.xy
as they will recieve the ...
arugments. There's also a couple of calls to undocumented functions (as far as I can find) like localWindow
and localBox
but I don't know what happens to them. I'd guess they're just ignored.
You can put the mgp parameter into the title() function to avoid having to reset your defaults afterwards. That way the parameter only acts on the label(s) added by the function. like this:
plot (x, y, type="o",xlab = "", #Don't include xlab in main plotylab = "Y values",cex.axis = "2",cex.lab = "2",las = 1)title(xlab="X values" ,mgp=c(6,1,0)) #Set the distance of title from plot to 6 (default is 3).