Luke Macaulay
Luke Macaulay

Reputation: 413

r - How can I get a lengthy label to fit using facet_grid in ggplot2?

Here is a reproducible example:

I first create a function based on this example https://github.com/hadley/ggplot2/wiki/labeller then provide the data and a graph

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

which produces a ggplot graph with a label for "PythonhasaREALLYWAYTOOlonglabel" that will oftentimes run off the edge of the plot.

I've tried playing with the various geom_bar widths from the following link but too no avail: How to increase the space between the bars in a bar plot in ggplot2?

Any help here? Thanks so much.

Upvotes: 2

Views: 3384

Answers (2)

Luke Macaulay
Luke Macaulay

Reputation: 413

While the answers so far are helpful in wrapping the text, the labels remain difficult to read. I've decided to go with adding a couple of zero values on both sides of the variable of interest and manually added a hyphenated name with a space for the factor of interest, changing "PythonhasaREALLYWAYTOOlonglabel" to "PythonhasaREALLY- WAYTOOlonglabel", which does what I'd like better so far. While it leaves perhaps too big of spaces on both sides of the "PythonhasaREALLY- WAYTOOlonglabel", it gives me the space I need.

Using the following code:

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)) 

There is probably a way to make the spaces on either side a bit narrower, but I'm not sure how to do that yet...

Upvotes: 0

IRTFM
IRTFM

Reputation: 263301

If you break the "reallylonglabel with spaces it will behave as intended:

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

OK. To your comment: here is a new labeller-function that will first split on any spaces (subject to low values of width) and then split on strings of length greater than 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)
  }
}

And if you supply less stringent arguments to "width" you can get this to accept width arguments in character-measured widths that are in the range of 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) )

Upvotes: 4

Related Questions