R specifying x-axis ticks for a line plot

◇◆丶佛笑我妖孽 提交于 2019-12-01 07:42:39

问题


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.

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)

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:


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.

Here is how I would proceed:

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

Resulting in the following plot:



来源:https://stackoverflow.com/questions/24414543/r-specifying-x-axis-ticks-for-a-line-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!