Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid overlapping axis labels in R

Tags:

plot

r

overlap

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 not 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

like image 333
R_User Avatar asked Jul 21 '11 15:07

R_User


People also ask

How do I stop Ggplot overlapping text?

Enter the ggrepel package, a new extension of ggplot2 that repels text labels away from one another. Just sub in geom_text_repel() in place of geom_text() and the extension is smart enough to try to figure out how to label the points such that the labels don't interfere with each other.

How do I turn off axis labels in R?

Data Visualization using R Programming When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title.

How do you change the axis titles in R studio?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


2 Answers

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.

like image 100
Backlin Avatar answered Sep 21 '22 08:09

Backlin


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.
like image 39
Richie Cotton Avatar answered Sep 19 '22 08:09

Richie Cotton