-1

请如何在条形文本中插入回车(ggplot2.0.9)

我试过了 :

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)

它返回

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

图中只有“xv”“xv”

谢谢你的帮助

4

1 回答 1

4

贴标机告诉您它需要一个长度为 1 的字符串,并且只返回第一个元素。

请注意,我已替换/r为新行\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)
} 

我认为应该可以使用正则表达式/字符串拆分方法来做到这一点。这行得通(但我确信有更好的)

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)
}
于 2012-08-13T03:59:12.500 回答