开发者

Avoid overlapping axis labels in R

开发者 https://www.devze.com 2023-03-21 20:23 出处:网络
I want to plot data in a graph with larger font-size for the lables. x = c(0:10) y = sin(x) + 10 plot ( x, y, type=\"o\",

I want to plot data in a graph with larger font-size for the lables.

x = c(0:10)
y = sin(x) + 10

plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

Unfortunately the numbers on the y-axis overlap the label for the y-axis. I tried to use mar, but that did no开发者_Go百科t work (By the way, how can I find out which graphic parameters can be directly used in the plot command and which have to be set with the par()-method? ).

How can I avoid that labels overlap?

Thanks for your help.

Sven


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:10
y = sin(x) + 10

par(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 plot
ylab = "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).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号