2

I am trying to label a ggplot with years but maintain 3 monthly tick marks, so require:

labels = c(0,"","","", 12,"","","", 24,"","","", 36...)

slightly neater is:

labels = c(0,rep("",3),12,rep("",3),24,rep("",3),36...)

It must be possible to programatically create this.

gsub("," , ",'','',''," ,"0,12,24,36" )

is close, but the only actual working solution I've managed is

labels=c()
for (i in 0:3){ labels<-c(labels,i*12,rep("",3)) }

I'm sure there must be an elegant solution can anyone come up with one?

2
  • I think your solution looks quite elegant. You could also do lapply: unlist(lapply(seq(0, 40, 12), c, rep("", 3))) Commented Apr 25, 2014 at 10:52
  • ggplot2:::interleave(letters[1:3], "", "", "") Commented Apr 25, 2014 at 11:21

2 Answers 2

2

I'd create a vector using seq and use the modulo operator (%%) to blank out entries I didn't want...

# Some data
df <- data.frame( x=seq(0,36) , y = rnorm(37) )

# Make quarterly labels  
lab <- seq(0,36,by=3)

# Blank everything that is not evenly divisible by 12 months
lab[ lab %% 12 > 0 ] <- ""

# Plot
ggplot( df , aes(x,y) ) + geom_point() +
  scale_x_continuous( breaks = seq(0,36,by=3) , labels = lab )

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

This will create the sequence for 10 years (40 entries)

lbls <- rep("", 40)   ## start empty vector
lbls[1+4*0:9] <- 12*0:9   ## selectively replace values with multiples of 12

I hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.