简体   繁体   中英

Segmenting X-axis label in an R plot

I've been working extensively with R lately and I have a nitpicky plotting question.

I've attached an image of my current plot as reference. As you can see, I've added vertical lines to segment parts of my data inputs. I have 200 'agents' and each of them comes from different categorical subsets which make them all a little different. So, my goal is to keep the bottom axis as the index of my 'agents' vector, but I'd like to add a label to each of my subdivisions at the bottom to make it a little clearer as to why I'm segmenting them with the vertical lines.

Any suggestions?

http://i.imgur.com/YGNdBhg.png?1?1971

You just need to call axis like this:

x = sin(1:100) + rnorm(100, 0,.125)
breaks = c(10,33,85, 96)

plot(x)
sapply(breaks, function(x){abline(v=x, lty=2)})
axis(1, breaks, as.character(breaks))

If you don't want the default ticks plotted at all (ie just the ticks in the "breaks" vector) you just need to modify this slightly:

plot(x, axes=F)
sapply(breaks, function(x){abline(v=x, lty=2)})
axis(1, breaks, as.character(breaks))
axis(2)
box()

You don't provide any example data or code, so the code I am sending is untested. I am calling the vector of vertical lines vertlines and the vector of labels labels . I define the midpoints of each category using the vertlines and the range of the agent values. Then I add them to the plot using the mtext() function. Give it a try.

vertlines <- c(40, 80, 120, 140, 160, 180)
labels <- letters[1:7]

labelx <- diff(c(1, vertlines, 200))/2 + c(1, vertlines)
mtext(labels, at=labelx, side=1, line=4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM