在不同级别绘制刻度线

时间:2017-04-11 22:33:06

标签: r plot labels

我想知道是否可以将刻度标签放在两个不同的级别,以避免标签之间重叠。 例如:

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)

2 个答案:

答案 0 :(得分:1)

你可以尝试以下方法:

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)

这基本上避免了重叠,首先在奇数位置绘制标签,然后在偶数位置绘制标签,但由于line = 1略低。

答案 1 :(得分:0)

您可以尝试进行两次axis次呼叫,一次是奇数月,一次是偶数,并将它们放在不同的line上。第一个axis调用只是绘制没有标签的刻度。

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