0

I need to assign certain names to the x-values of a percentage growth time series plot. I know how to remove the values but I want the names "08-09 q1","08-08 q2","08-09 q3","08-09 q4","09-10 q1",...,"14-15 q2" to replace them so that each value on the x-axis is directly below the point plotted. How can I do this?

plot.ts(percentagegrowth1,xaxt="n",xlab="Quarter",ylab="Percentage growth",main="Year-over-Year Quarterly Percentage Growth") 

for(i in 1:length(percentagegrowth1r))  
{
  text(x=i,y=percentagegrowth1r[i],labels=percentagegrowth1r[i],cex=0.5,font=2,pos=1)  
  text(x=i,y=percentagegrowth1r[i],labels=percentagegrowth1r[i],cex=0.5,font=2,pos=3)
} 

lines(percentf1,col="red")  
points(percentagegrowth1r,col="blue")  
dates<-c(1.1,1.2,1.3,1.4,2.1,2.2,2.3,2.4,3.1,3.2,3.3,3.4,4.1,4.2,4.3,4.4,5.1,5.2,5.3,5.4,6.1,6.2,6.3,6.4,7.1,7.2) 

for(i in 1:length(dates)) { 
  axis(1,at=i,labels=dates[i]) 
} 

legend("topright",c("x-values=Year comparison.Quarter e.g 1.1=08-09 q1","Red=Forecasted data"))  

percentagegrowth1r=
-37.1 9.6 -8.9 71.8 111.8 106.4 65.4 68.8 53.0 40.6 22.5 36.9 30.8 14.5 23.6 38.3 16.1 29.2 39.0 4.0 -6.3 46.0 32.7 32.8 72.4 13.5
percentf1= NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 32.67955 32.78714 72.44055 13.48962

This is an image of the graph I have plotted:

enter image description here

I want the value 1.1 replaced with "08-09 q1", 1.2 replaced with "08-08 q2" , 1.3 replaced with "08-08 q3", 1.4 replaced with "08-09 q4", 2.1 replaced with "09-10 q1", ... , 7.1 replaced with "14-15 q1" and 7.2 replaced with "14-15 q2"

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    I have no idea what you asking. Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [take a tour](http://stackoverflow.com/tour) – David Arenburg Sep 08 '14 at 12:15
  • sorry this is my first time in using this site. – Summer-Jade Gleek'away Sep 08 '14 at 12:28
  • NP, just follow the links I've provided above and rewrite your question ASAP. Otherwise it will be closed by the community. – David Arenburg Sep 08 '14 at 12:30
  • I have edited it, I hope this gives a better explanation of what I mean – Summer-Jade Gleek'away Sep 08 '14 at 12:37
  • Please also add the code you have used in order to create this plot – David Arenburg Sep 08 '14 at 12:44
  • possible duplicate of [R: How do I set what plot() labels the x-axis with?](http://stackoverflow.com/questions/1395577/r-how-do-i-set-what-plot-labels-the-x-axis-with) – hrbrmstr Sep 08 '14 at 12:45
  • @Summer-JadeGleek'away The edit does not include any sample data. It would be nice if you put together sufficient code/data that we can copy and paste it into R to re-create the plot you've drawn. (It doesn't have to be your exact data, just some that is in the same format) – MrFlick Sep 08 '14 at 12:46
  • @MrFlick my code is extremely long due to it running arima so you wont be able to run my code...sorry I am new to this and am really unsure of everything im doing :( – Summer-Jade Gleek'away Sep 08 '14 at 12:50
  • @hrbrmstr I have reviewed this question and I have tried to apply it to my data however I cant get it to work – Summer-Jade Gleek'away Sep 08 '14 at 12:52
  • 1
    We don't care *how* the data for the plot was generated so much as what the values and classes involved actually are. Seems like you should just be able to post `dput(percentagegrowth1)` to share the time series object you are actually plotting. Easy ways to do this are in the "reproducible example" link that @DavidArenburg provided. – MrFlick Sep 08 '14 at 12:55
  • I have added in my data beneath the code (this is just the percentages I produced from previous data but is what is plotted) – Summer-Jade Gleek'away Sep 08 '14 at 13:04
  • Is the second one in your sequence: "08-09 q1","08-08 q2","08-09 q3","08-09 q4" a mistake and should be "08-09 q2"? I can't understand the logic otherwise... – Spacedman Sep 08 '14 at 13:49
  • sorry yes that is a typo, its ok now, I have figured it out :) – Summer-Jade Gleek'away Sep 08 '14 at 14:10

2 Answers2

1

The problem appears to be one of generating the labels for the given year and quarter. How's this:

yr=function(i){paste(sprintf("%02d-%02d",i,i+1))}

that lets us generate arbitrary year labels:

yr(6:14)
[1] "06-07" "07-08" "08-09" "09-10" "10-11" "11-12" "12-13" "13-14" "14-15"

then quarters is easy:

qlabels =
function(yrs){
  q = rep(1:4, length(yrs))
  sprintf("%s q%s", rep(yr(yrs),rep(4,length(yrs))), q)
}

producing:

> qlabels(8)
[1] "08-09 q1" "08-09 q2" "08-09 q3" "08-09 q4"
> qlabels(8:10)
 [1] "08-09 q1" "08-09 q2" "08-09 q3" "08-09 q4" "09-10 q1" "09-10 q2"
 [7] "09-10 q3" "09-10 q4" "10-11 q1" "10-11 q2" "10-11 q3" "10-11 q4"

So then add the axis labels, cutting the sequence short because you end at a q2:

> plot.ts(percentagegrowth1r,xaxt="n",xlab="Quarter",ylab="Percentage growth",main="Year-over-Year Quarterly Percentage Growth") 
> axis(1,1:length(dates), qlabels(8:14)[1:26])

axis doesn't plot all the values because they don't fit without overlapping. You can adjust the cex parameter:

> plot.ts(percentagegrowth1r,xaxt="n",xlab="Quarter",ylab="Percentage growth",main="Year-over-Year Quarterly Percentage Growth") 
> par(cex=0.5)
> axis(1,1:length(dates), qlabels(8:14)[1:26])

detail of tiny labels

Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

I figured out the answer, in case anyone else has the same problem :)

first assign a name to each number in the plotted vector:

names(percentagegrowth1)<-c("08-09 q1","08-09 q2","08-09 q3","08-09 q4","09-10 q1","09-10 q2","09-10 q3","09-10 q4","10-11 q1","10-11 q2","10-11 q3","10-11 q4","11-12 q1","11-12 q2","11-12 q3","11-12 q4","12-13 q1","12-13 q2","12-13 q3","12-13 q4","13-14 q1","13-14 q2","13-14 q3","13-14 q4","14-15 q1","14-15 q2")

then plot the graph:

plot.ts(percentagegrowth1,xaxt="n")

then assign the x-values:

axis(1,at=seq(1,length(percentagegrowth1),by=1),labels=names(percentagegrowth1))
Jaap
  • 81,064
  • 34
  • 182
  • 193