S Rivero
S Rivero

Reputation: 708

Plot with ticks at different levels

I would like to know if it is possible to put the ticks labels at two different levels to avoid overlapping between labels. For example here:

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

Upvotes: 0

Views: 42

Answers (2)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

You could try to do two axis calls, one for odd months and one for even ones and put them on different line. The first axis call is simply to draw the ticks, without labels.

a=1:10
a_even <- a[a %% 2==0]
a_odd <- a[a %% 2==1]
b=c("January","February","March","April","May","June","July","August","September","October")
barplot(a,space=0,axes=F)
ticks=a
axis(side=1, at =ticks, labels=FALSE,line = 1)
axis(side=1, at =ticks[a_odd], labels=b[a_odd],line = 1,tick = FALSE)
axis(side=1, at =ticks[a_even], labels=b[a_even],line = 2,tick = FALSE)

enter image description here

Upvotes: 0

Vandenman
Vandenman

Reputation: 3176

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.

Upvotes: 1

Related Questions