Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: label_parsed and label_wrap_gen(multi_line=FALSE) together

Tags:

r

ggplot2

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.

like image 538
quibble Avatar asked Sep 14 '25 18:09

quibble


1 Answers

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))
like image 192
AK88 Avatar answered Sep 16 '25 07:09

AK88