Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Fit Long Text into Ggplot2 facet Titles

Tags:

r

ggplot2

I have a quick question about facet_wrap labels in ggplot2. Below is a simple data frame. One of the variables (the facet variable) is very long. I want to find a simple way to fit all the text in each facet label. I'm sure there must be some kind of wrap text function or multiple line option? I'm hoping for a method that is not too complex or doesn't really require any other packages if possible.I'm still relatively new with R and hoping for a short and elegant answer within ggplot2.

Q1<-c("Dissatisfied","Satisfied","Satisfied","Satisfied","Dissatisfied","Dissatisfied","Satisfied","Satisfied")

Q2<-c("Dissatisfied","Dissatisfied","Satisfied","Dissatisfied","Dissatisfied","Satisfied","Satisfied","Satisfied")



Year<-c("This is a very long variable name This is a very","This is another really long veriable name a really long","THis is a shorter name","Short name","This is also a long variable name again","Short name","Short name","Another kind of long variable name")

Example<-data.frame(Service,Year,Q1,Q2)

ExampleM<-melt(Example,id.vars=c("Service","Year"))

ggplot(ExampleM, aes(x=variable, fill=value)) + 
   geom_bar(position="dodge")+
   facet_grid(~Year)
like image 851
Mike Avatar asked May 11 '16 22:05

Mike


People also ask

How to change facet Label size in ggplot?

If we want to modify the font size of a ggplot2 facet grid, we can use a combination of the theme function and the strip. text. x argument. In the following R syntax, I'm increasing the text size to 30.

How do you wrap facet labels?

How can I wrap facet label text so that long labels are spread across two rows? Use label_wrap_gen() in the labeller argument of your faceting function and set a width (number of characters) for the maximum number of characters before wrapping the strip.

What is facet wrap in ggplot2?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


2 Answers

A commonly used package already has this functionality: use stringr::str_wrap().

library(stringr)
library(plyr)
library(dplyr)

var_width = 60
my_plot_data <- mutate(my_plot_data, pretty_varname = str_wrap(long_varname, width = var_width))

And then proceed with your plot.

like image 64
Taylor White Avatar answered Sep 21 '22 00:09

Taylor White


You can use strwrap to create line breaks. Here's an example:

library(reshape2)
library(ggplot2)

Example<-data.frame(Year,Q1,Q2, stringsAsFactors=FALSE)

ExampleM<-melt(Example,id.vars=c("Year"))

# Helper function for string wrapping. 
# Default 20 character target width.
swr = function(string, nwrap=20) {
  paste(strwrap(string, width=nwrap), collapse="\n")
}
swr = Vectorize(swr)

# Create line breaks in Year
ExampleM$Year = swr(ExampleM$Year)

ggplot(ExampleM, aes(x=variable, fill=value)) + 
  geom_bar(position="dodge") + 
  facet_grid(~Year)

enter image description here

like image 23
eipi10 Avatar answered Sep 18 '22 00:09

eipi10