J.Lai
J.Lai

Reputation: 21

Cannot italicize facet labels with labeller = label_parsed

I have a ggplot

s = ggplot(final, aes(y = avg,x=factor(dose),fill = factor(mo))) + 
        facet_grid(.~gene, labeller = label_parsed) +
        geom_bar(position = "dodge", stat = "identity", color = "black")

where final is a data.frame containing 4 columns: mo, dose, gene and avg.

I've been trying to italicize the facet labels with

s + theme(strip.text.x = element_text(face = "italic", size = 10, colour = "white"))

to no avail. Size and colour can be changed with no problems.

However, once I remove "labeller" argument from facet_grid(), the font face can be changed accordingly. Is this a bug?

Although I'll work around this problem by setting them individually as suggested here, I'm sure theme() was there for a very good reason.

Upvotes: 2

Views: 3053

Answers (1)

bhuss
bhuss

Reputation: 119

As you said, you need something like what has been shown here

In fact you have to change directly your levels with italic() then you use labeller=label_parsed in ggplot

an example:

factor1=rep(letters[1:3], each=3)
factor2=rep(1:3,times=3)
x=rep(1,9)
y=1:9
df=cbind.data.frame(factor1,factor2,x,y)

i revalue the levels:

levels(df$factor1)= c("a"=expression(paste("factor_", italic("a"))),
               "b"=expression(paste("factor_", italic("b"))),
               "c"=expression(paste("factor_", italic("c"))))

And adapt ggplot:

ggplot(df, aes(x=x, y=x))+facet_grid(factor2~factor1, labeller=label_parsed)+geom_point()

enter image description here

Upvotes: 3

Related Questions