R Draw All Axis Labels (Prevent Some from Being Skipped)

R Draw All Axis Labels (Prevent Some From Being Skipped)

?axis tells you that:

The code tries hard not to draw overlapping tick labels, and so will omit labels where they would abut or overlap previously drawn labels. This can result in, for example, every other tick being labelled. (The ticks are drawn left to right or bottom to top, and space at least the size of an ‘m’ is left between labels.)

Play with cex.axis so that labels are small enough to fit without overlapping

labs <-c("0\n9.3%","1\n7.6%","2\n5.6%","3\n5.1%","4\n5.7%","5\n6.5%","6\n7.3%",
"7\n7.6%","8\n7.5%","9\n7%", "10\n6.2%","11\n5.2%","12\n4.2%",12:27)
plot(1:27,xaxt = "n")
axis(side=1, at=1:27, labels=labs[0:27],cex.axis=0.35)

If you widen you graph (manually by dragging or programmatically), you can increase the size of your labels.

How to not show all labels on ggplot axis?

Is your year column numeric? You can add scale_x_continuous with a breaks argument to specify where the x axis ticks should be. I can't tell what the range of years is in the image, but if it's from 1900 to 2000 (e.g.) you can do something like this:

ggplot(prova, aes(x=year, y=mass..g.)) +
geom_line(aes(group=1)) +
scale_x_continuous(breaks=seq(1900, 2000, 10))

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.

Labelling every category in R bar plot / box plot: `gap.axis` equivalent?

In R 4.1.0, barplot() passes gap.axis is passed with the ... to axis(), producing the required effect. To suppress the warnings resulting from gap.axis also being passed to other plotting functions (reported as a bug), use:

suppressWarnings(barplot(setNames(1:10, 1:10),
cex.names = 2, gap.axis = -10))

[Edited:]
As of R4.1.0-patched80484, gap.axis is natively supported by boxplot(), thanks to Martin Maechler.

As a workaround in older versions of R, use:

dat <- matrix(1:100, 10, 10)
boxplot(dat, axes = FALSE)
axis(1, seq_len(ncol(dat)), colnames(dat),
cex.axis = 2, gap.axis = -10)

showing different scaling in y axis when plotting two line graph in one plot

You have to add your data, but so far I understand you want something like this?

plot(NULL, xlim=c(2007, 2014), ylim=c(78,86), xlab="Training Years", ylab="Performances")
axis(side=2, at=c(78:86), labels=c(78:86))

x1 <- c(2007:2014)
y1 <- runif(8,78,86)

lines(x1, y1, col="blue")
points(x1,y1, col="blue")

y2 <- runif(8,78,86)
lines(x1, y2, col="red")
points(x1,y2, col="red")

Sample Image

Avoid wasting space when placing multiple aligned plots onto one page

Here is a slight modification of the general plot you show, assuming that the y and x axis labels pertain to all plots. It uses an outer margin to contain the axis labelling, which we add with title() using argument outer = TRUE. The effect is somewhat like the labelling in ggplot2 or lattice plots.

The key line here is:

op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,1) + 0.1)

which sets plot parameters (the values in place prior to the call are stored in op). We use 5 and 4 lines on sides 1 and 2 for the outer margin, which is the usual number for the mar parameter. Plot region margins (mar) of 1 line each are added to the top and right sides, to give a little room between plots.

The axis labels are added after the for() loop with

title(xlab = "Some Categories",
ylab = "Some Values",
outer = TRUE, line = 3)

The entire script is:

set.seed(42)
catA <- factor(c("m100", "m500", "m1000", "m2000", "m3000", "m5000"))
catB <- factor(20:28)
samples <- 100
rsample <- function(v) v[ceiling(runif(samples, max=length(v)))]
Tab <- data.frame(catA = rsample(catA),
catB = rsample(catB),
valA = rnorm(samples, 150, 8),
valB = pmin(1,pmax(0,rnorm(samples, 0.5, 0.3))))
op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,1) + 0.1)
for (i in 0:3) {
x <- Tab[[1 + i %% 2]]
plot(x, Tab[[3 + i %/% 2]], axes = FALSE)
axis(side = 1,
at=1:nlevels(x),
labels = if (i %/% 2 == 1) levels(x) else FALSE)
axis(side = 2, labels = (i %% 2 == 0))
box(which = "plot", bty = "l")
}
title(xlab = "Some Categories",
ylab = "Some Values",
outer = TRUE, line = 3)
par(op)

which produces

Sample Image

Why ACF not plotting lags

The data is a time series by month. Forty lags spans a range of 40 months, or 3.33 years. The time unit on the x-axis is denominated in years and you're seeing lags of 0 to 40 months in the graph.

As another example, if you run acf(AirPassengers, lag.max=12) you can see that the x-axis has lags from 0 to 12 months and the axis is labeled from zero to 1 year.

Sample Image

You can relabel the axis if you wish. For example:

mx=40
acf(AirPassengers, lag.max=mx, xaxt="n", xlab="Lag (months)")
axis(1, at=0:mx/12, labels=0:mx)

Sample Image

Plot with ticks at different levels

you could try the following:

a=1:10
b=c("January","February","March","April","May","June","July","August","September","October")
barplot(a,space=0,axes=F)
ticks=a

# indices of even ticks
idx <- seq(2, length(ticks), 2)

# b2 only contains the odd labels.
b2 <- b
# Empty space so a small line gets drawn at omitted labels
b2[idx] <- ""

# draw odd labels
axis(side=1, at =ticks, labels=b2, line = 0)
# same command for even ticks, lwd = 0 suppresses drawing the x-axis twice
axis(side=1, at =ticks[idx], labels=b[idx], line = 1, lwd = 0)

This basically circumvents overlapping by first drawing the labels at odd positions and then drawing the labels at even positions but slightly lower due to line = 1.



Related Topics



Leave a reply



Submit