I want to combine the labels of two facet variables into one line, but the label contains math expression.
library(ggplot2)
dat <- data.frame(var1=rep(c("alpha[Q]==2","alpha[Q]==5"),each=2),var2=rep(c("beta[Q]==2","beta[Q]==5"),2),x=1:4,y=c(1,-1000,1000,100))
# plot 1
ggplot(dat,aes(x,y))+geom_point()+facet_wrap(~var1+var2,scale="free",labeller=label_parsed)
# plot2
ggplot(dat,aes(x,y))+geom_point()+facet_wrap(var1~var2,scale="free",labeller=label_wrap_gen(multi_line=FALSE))
plot 1 shows expression correctly, but the two variables are not combined. plot 2 combines the variables but the expression is not right. I want plot 2 to show the expression correctly.
I think you are looking for the following .multi_line = FALSE
(notice the .
):
ggplot(dat,aes(x,y))+
geom_point()+
facet_wrap(~var1+var2,scale="free",
labeller = labeller(.cols = label_parsed, .multi_line = FALSE))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With