Set the various positions of the axis marks and marks in the line cabinet

I would like to rebuild / offset the x axis and its associated bar font marks. It should be simple, but it's hard for me to find the answer. Below are sample data with 24 categories.

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

I can easily create a barcode with label marks aligned at the midpoints of the bar using the code below:

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

I can also shift the entire X axis (marks and ticks) to align it with the external panel using the following (although now this does not allow the last stroke to be included in the axis):

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

While I would like the x-axis and associated labels to be aligned with the borders of the strokes (i.e. the tick mark on both sides of the bar, and not in the center), I want the x-axis labels to remain at the midpoints of the bar . Is there an easy way to achieve this?

+4
1

, ?

# 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

+7

All Articles