简体   繁体   English

在R图中分割X轴标签

[英]Segmenting X-axis label in an R plot

I've been working extensively with R lately and I have a nitpicky plotting question. 我最近一直在与R进行广泛的合作,并且有一个挑剔的绘图问题。

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. 我有200个“代理”,每个代理都来自不同的类别子集,这使得它们都有些不同。 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. 因此,我的目标是将底轴保留为“ agents”向量的索引,但我想在底部的每个细分中添加一个标签,以使其更清楚地说明为什么要进行细分他们与垂直线。

Any suggestions? 有什么建议么?

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

You just need to call axis like this: 您只需要这样调用axis

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: 如果您根本不想绘制默认刻度(即仅“ breaks”矢量中的刻度),则只需稍作修改即可:

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 . 我打电话垂直线的矢量vertlines和标签的载体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. 然后,使用mtext()函数将它们添加到绘图中。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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