4
votes

so I am struggling to get a plot working like I want. I have a facet_grid where the variables facetted is determined dynamically in a shiny app...

facet_grid(facetFormula, labeller = label_both)

where...

facetFormula <- as.formula(paste(input$filter2Var, "~", input$filter1Var))

this works fine, except that i'd rather a linebreak as the variable "name: value" separator instead of the colon. i've poked around with other arguments (multi_line, sep), using label_both() or label_wrap_gen() or labeller() instead of label_both no parenthesis... and am getting no where, probably stumbling over the already complex issue of dynamic variables to be facetted by. i've tried treating arguments to these various functions with !!sym() or as.formula(), but i really don't know what i am doing and probably messing up several things in trying to just add some simple text wrapping to my facet labels. any help is much appreciated!

UPDATE...

cases <- c("case1_has_long_name", "case2_long_too", "case3_long_as_well", "case4_also_long", "case5_long")
the_first_variable <- cases[round(runif(100,1,3))]
variable_number_two <- cases[round(runif(100,1,5))]
var1 <- "the_first_variable"
var2 <- "variable_number_two"
facetFormula <- as.formula(paste(var1, "~", var2))
myX <- runif(100,0,10)
myY <- runif(100,-5,5)
myData <- data.frame(myX, myY, the_first_variable, variable_number_two)
ggplot(myData, aes(x = myX, y = myY)) +
  geom_point(alpha = .5) +
  facet_grid(facetFormula,
             labeller = label_both)

this generates a plot with my issue, where the facet labels are too big. i just want to learn how to make the labels wrap. was thinking as a simple start, instead of ":" as the separator between variable name and variable value, i could use "\n" as the seperator. the awkwardness of specifying my facet variables as variable themselves comes from them being dynamically defined in a shiny app.

1
Welcome to stack overflow. It’s easier to help if you make your question reproducible: include a minimal dataset in the form of an object for example if a data frame as df <- data.frame(…) where … is your variables and values or use dput(head(df)). Include the code you have tried and set out your expected answer. These links should be of help: minimal reproducible example and How to AskPeter
thanks Peter, you're absolutely right... i edited my question with some better code and dataAlex

1 Answers

2
votes

Wrapping the facet labels could be achieved like so:

A labeller takes as its first argument a df with the variable names and labels. In your case a df with one column. The column contains the labels, the column name is the var name.

To wrap the labels I use a wrapper function around label_both where I first wrap the labels before passing the manipulated df to label_both. To make the wrapping work I replaced all underscores by spaces.

library(ggplot2)

my_label <- function(x) {
  # Wrap var names
  names(x)[[1]] <- stringr::str_wrap(gsub("_", " ", names(x)[[1]]), 10)
  # Wrap value labels
  x[[1]] <- stringr::str_wrap(gsub("_", " ", x[[1]]), 10)
  # Call label both with sep "\n"
  label_both(x, sep = "\n")
}

ggplot(myData, aes(x = myX, y = myY)) +
  geom_point(alpha = .5) +
  facet_grid(facetFormula, labeller = my_label)