-1

please how to insert a carriage return in strip text (ggplot 2.0.9)

I tried :

mf_labeller <- function(var, value){
  value <- as.character(value)
    if (var=="Nerves") { 
    value[value=="xv1"] <- c("xv","/r", "1")
    value[value=="xv2"]   <- c("xv","/r", "2")

  }
  return(value)
} 

facet_grid(..., labeller=mf_labeller)

It returns

Warning in value[value == "xv1"] <- c("xv","/r", "1") : number of items to replace is not a multiple of replacement length
Warning in value[value == "xv2"] <- c("xv","/r", "2") : number of items to replace is not a multiple of replacement length

and only "xv" "xv" on the figure

Thanks for your help

plannapus
  • 18,529
  • 4
  • 72
  • 94
user1594303
  • 127
  • 1
  • 2
  • 8
  • I think you need to `paste` these, not `c`ombine. Also, `\n` will give you a new line `\r` does not appear to. – mnel Aug 13 '12 at 03:42

1 Answers1

4

The labeller is telling you that it expecting a string of length 1, and only returning the first element.

Note that I have replaced /r with the new line \n

mf_labeller <- function(var, value){
  value <- as.character(value)
    if (var=="Nerves") { 
    value[value=="xv1"] <-  paste("xv","\n", "1")
    value[value=="xv2"]   <- paste("xv","\n", "2")

  }
  return(value)
} 

I think should be possible to do this with a regex / string split approach. This works (but I'm sure there is better)

mf_labeller <- function(var, value){
  value <- as.character(value)
  if (var=="Nerves") { 
    unscramble <- unlist(strsplit(split = '',value))
    value <- paste(paste0(unscramble[1:2],collapse=''), '\n', unscramble[3])

  }
  return(value)
}
mnel
  • 113,303
  • 27
  • 265
  • 254