R指定线图的x轴刻度 [英] R specifying x-axis ticks for a line plot
问题描述
我有一些时间序列,其中200个值与一个时间间隔内的200个不同时间相关联.绘制线图后,我想手动告诉R仅显示我指定的某些特定值的x轴刻度.然后,我想指定一个不同的名称,即日期字符串,在这些刻度上绘制而不是数值.我已经看过axis(),但是在将整个过程组合在一起时遇到了麻烦.我希望绘制整个折线图,而不仅仅是我指定的x标签.
I have some time series with 200 values associated with 200 different times within an interval. After plotting the line plot, I would like to manually tell R to only show x axis ticks for some specific values I specify. Then, I would like to specify a different name, ie a date string, to be plotted at these ticks instead of the numerical value. I've looked at axis() but i'm having trouble putting this whole process together. I want the entire line graph to be plotted, not just the x labels i'm specifying.
例如: 如果我有: y = c(227,342,121,275,354,999,221,475,867,347,541) x = c(1,5,10,15,20,25,30,35,40,45,50)
For example: If i have: y = c(227, 342, 121, 275, 354, 999, 221, 475, 867, 347, 541) x = c(1, 5, 10, 15, 20, 25, 30 ,35, 40, 45, 50)
我想要plot(x,y,"l"),但x轴仅在c(10,30,40)处滴答.对于这些刻度,我想分别关联c("March","June","August"),并将这些名称作为这三个刻度的标签.
I want plot(x, y, "l") but with x axis ticks only at c(10, 30, 40). For these ticks, I would like to associate c("March", "June", "August") respectively and have these names as the labels for these three ticks.
推荐答案
您将需要使用手动轴创建方法.可以通过(1)禁用自动轴创建(在对plot
的调用中使用xaxt='n'
)和(2)对axis
函数进行自定义调用来实现.
You will need to resort to the manual axis creation. This can be done by (1) disabling the automatic axis creation (with xaxt='n'
in the call to plot
) and (2) make a custom call to the axis
function.
这是我要进行的操作:
y = c(227, 342, 121, 275, 354, 999, 221, 475, 867, 347, 541)
x = c(1, 5, 10, 15, 20, 25, 30 ,35, 40, 45, 50)
plot(x, y, "l", xaxt='n')
axis(1,at=c(10,30,40),labels=c("March", "June", "August"))
结果如下图所示:
这篇关于R指定线图的x轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!