r - Auto wrapping of labels via labeller=label_wrap in ggplot2 -


I want to automatically wrap my label in ggplot2. Wrote how to write the function (1) for this, but unfortunately I do not know where to put labeleller = label_wrap in my code (2).

(1) Function by now

  label_wrap & lt; - Function (Variable, Value) {Lapli (Stupa (As Checher (Value), Width = 25, Simplified = False), Paste, Fall = "\ n")}  

( 2) Code example

  df = data.frame (x = c ("label", "tall label", "very, very long label"), y = c (10, 15, 20)) ggplot (df, aes (x, y)) + geom_bar (stat = "identity")  

I want to wrap some more labels here I am

You do not need the ' label_wrap function instead of Use the str_wrap function from the Stringer package.

You do not provide your df data frame, so I create a simple data frame, which contains your label, then on the label str_wrap Apply the function.

  Library (ggplot2) library (stringer) df = data.frame (x = c ("label", "tall label", "very, very long label"), y = c ( 10, 15, 20)) df df $ newx = str_wrap (df $ x, width = 10) df  

Now to apply the label to a ggplot chart: The first chart is of basic Uses label; The second chart uses modified labels; And for the third chart, the labels are modified in the call in ggplot. Ggplot (df, aes (x, y)) + xlab ("") + ylab ("Number of participants +" GOMLbar (state = "identification") ggplot (df, aes ( + Xlab ("") + ylab ("New", "Y"), + XLab ("Number of Participants") + GOm_bar (State = "Identification") GG Plot (DF, AES (X, Y) Number of participants ") + geom_bar (stat =" identity ") + scale_x_discrete (labels = function (x) str_wrap (x, width = 10))


Comments