2013-10-28 51 views
4

我想重新排列/偏移一個條形圖的x軸和相關的刻度線。這應該很簡單,但我無法找到答案。以下是24個類別的一些示例數據。在一個條形圖中設置軸標籤和刻度線的不同位置

xval = c(1:24) 
count = c(0.03,0.03,0.08,0.06,0.11,0.4,0.3,0.5,0.5,0.6,0.4,0.1,0.1,0.4,0.2,0.1,0.06,0.05,0.03,0.02,0.01,0.03,0.01,0.02) 
df = as.data.frame(cbind(xval, count)) 

我可以很容易地生產具有在使用下面的代碼條的中點對準刻度標記一個barplot:

mp <- barplot(df$count, space=0, axes=FALSE) 
axis(side=2, pos=-0.2) 
axis(side=1, at =mp, labels=df$xval) 

我也可以移動整個x軸(標籤和蜱)與對準的使用下面的杆的外側(雖然這現在無法摻入最後扎入軸):

axis(side=1, at =mp-0.5, labels=df$xval) 

雖然我想x軸和相關的刻度線與杆b被對準邊界(即在酒吧的任一側而不是中心的刻度線),我希望X軸標籤保持在酒吧中點。有沒有簡單的方法來實現這一目標?

回答

6

這是你在找什麼?

# create positions for tick marks, one more than number of bars 
at_tick <- seq_len(length(count) + 1) 

# plot without axes 
barplot(count, space = 0, axes = FALSE) 

# add y-axis 
axis(side = 2, pos = -0.2) 

# add x-axis with offset positions, with ticks, but without labels. 
axis(side = 1, at = at_tick - 1, labels = FALSE) 

# add x-axis with centered position, with labels, but without ticks. 
axis(side = 1, at = seq_along(count) - 0.5, tick = FALSE, labels = xval) 

enter image description here

+0

@亨裏克:非常感謝。輝煌的,這種排序標籤的位置,雖然我想軸線和刻度線,如果可能的話,延伸到最後的酒吧? – Emily

+0

@ Henrik:那很完美。非常感謝! – Emily

相關問題