Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the comma in facet labels when ".multi_line = FALSE"

ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      facet_wrap(c("cyl", "drv"), labeller = labeller(.multi_line = FALSE))

I would like to replace the comma with space in labels.

Code output

like image 838
Zuooo Avatar asked Mar 05 '23 04:03

Zuooo


2 Answers

You can do something like this -

 ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      facet_wrap(c("cyl", "drv"), labeller = function (labels) {
      labels <- lapply(labels, as.character)
      a <-  do.call(paste, c(labels, list(sep = ",")))
      list(gsub("\\,"," ",a))
    })

Note- We can pass any custom function by using this method.

Output-

Output

like image 131
Rushabh Patel Avatar answered Mar 07 '23 00:03

Rushabh Patel


mpg$label <- paste(mpg$cyl, mpg$drv)

ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      facet_wrap(~label)

Final plots

like image 26
Zuooo Avatar answered Mar 07 '23 01:03

Zuooo