问题
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