2

这是一个可重现的示例:

我首先基于此示例创建一个函数https://github.com/hadley/ggplot2/wiki/labeller然后提供数据和图表

label_wrap_gen <- function(width = 100) {
  function(variable, value) {
    lapply(strwrap(as.character(value), width=width, simplify=FALSE), 
           paste, collapse="\n")
  }
}

Data <- data.frame(Language=c("C++","C++","C++", "Java","Java","Java","Java", "PythonhasaREALLYWAYTOOlonglabel"), 
                Files=c(400, 210, 35,55,330,220,213,76), 
                Difficulty=c("a","b","c","d","e","f","g","h"), 
                stringsAsFactors=FALSE)

g <- ggplot(Data,aes(x=Difficulty,y=Files,fill=Difficulty))  #replaced fill=feetype,
h <- g + geom_bar(stat="identity",position="dodge") +   facet_grid(.~ Language, scales = "free_x", space="free",labeller=label_wrap_gen(width=.1)) 
h

它会生成一个带有“PythonhasaREALLYWAYTOOlonglabel”标签的 ggplot 图,该图通常会超出图的边缘。

我已经尝试使用geom_bar以下链接中的各种宽度,但也无济于事: 如何增加 ggplot2 中条形图中条形之间的空间?

这里有什么帮助吗?非常感谢。

4

2 回答 2

4

如果您用空格打破“reallylonglabel”,它将按预期运行:

 "Python has a REALLY WAY TOO long label"  # will be handled correctly

好的。对您的评论:这是一个新的labeller- 函数,它将首先拆分任何空格(受低值的影响width),然后拆分长度大于 5 的字符串。

label_wrap_gen2 <- function(width = 100) {
  function(variable, value) {
    inter <- lapply(strwrap(as.character(value), width=width, simplify=FALSE), 
           paste, collapse="\n")
    inter <- gsub("(.{5})", "\\1\n",inter)
  }
}

如果您为“宽度”提供不太严格的参数,您可以让它接受 5-10 范围内的字符测量宽度中的宽度参数:

label_wrap_gen3 <- function(width = 100) {
  function(variable, value) {
    inter <- lapply(strwrap(as.character(value), width=width, simplify=FALSE), 
           paste, collapse="\n")
    inter <- gsub(paste0("(.{",width,"})"), "\\1\n",inter)
  }
}

# Seems to deliver expected results when called with:
  ... + facet_grid(..., labeller=label_wrap_gen3(width=5) )
于 2013-07-12T05:33:57.807 回答
0

虽然到目前为止的答案有助于包装文本,但标签仍然难以阅读。我决定在感兴趣的变量的两侧添加几个零值,并手动添加一个带有空格的连字符名称,将“PythonhasaREALLYWAYTOOlonglabel”更改为“PythonhasaREALLY-WAYTOOlonglabel”,它做了什么到目前为止我想要更好。虽然它可能在“PythonhasaREALLY-WAYTOOlonglabel”的两侧留下了太大的空间,但它给了我需要的空间。

使用以下代码:

label_wrap_gen <- function(width = 100) {
  function(variable, value) {
    lapply(strwrap(as.character(value), width=width, simplify=FALSE), 
           paste, collapse="\n")
  }
}

Data <- data.frame(Language=c("C++","C++","C++", "Java","Java","Java","Java", "PythonhasaREALLY- WAYTOOlonglabel","PythonhasaREALLY- WAYTOOlonglabel","PythonhasaREALLY- WAYTOOlonglabel"), #note that I add a hyphen here and two placeholders that will have 0 values
                   Files=c(400, 210, 35,55,330,220,213,0,76,0), #note that I add two 0 values here
                   Difficulty=c("a","b","c","d","e","f","g","h","i","j"), 
                   stringsAsFactors=FALSE)

Data
g <- ggplot(Data,aes(x=Difficulty,y=Files,fill=Difficulty))  #replaced fill=feetype,
h <- g + geom_bar(stat="identity",position="dodge") +   facet_grid(.~ Language, scales = "free_x", space="free",labeller=label_wrap_gen(width=.1)) 

可能有一种方法可以使两边的空间变窄一些,但我不知道该怎么做......

于 2013-07-12T21:40:38.337 回答