2

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.

Community
  • 1
  • 1
J.Lai
  • 21
  • 1
  • 3

1 Answers1

3

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

Community
  • 1
  • 1
bhuss
  • 119
  • 1
  • 1
  • 11
  • Tiny question. What does cbind.data.frame() do that data.frame() does not do? – lawyeR Dec 02 '15 at 12:09
  • Thanks user. I want to italicize all the facet labels and thus using theme() would be the straightforward method. – J.Lai Dec 02 '15 at 12:18
  • @lawyeR from the help: alling this function is nearly equivalent to calling data.frame. [...] This is a method for the generic function cbind. Calling cbind when at least one argument is a data frame is equivalent to calling data.frame with the same arguments, with the exception of the control arguments check.names and deparse.level. Unlike data.frame, column names are not changed. If you want variable names to be made unique, call data.frame instead of cbind. – bhuss Dec 02 '15 at 12:47
  • @J.lai: yes i understand. I have been looking for this some time ago, and i could not find anything else than to rename levels seperately. Sorry – bhuss Dec 02 '15 at 12:51
  • @BerengereHusson. Nevertheless, your solution is less wordy than atop ^^. thanks a bunch! – J.Lai Dec 02 '15 at 13:43
  • @BerengereHusson. another thought just came to my mind. it could be that label_parsed overwrites theme() with respect to font face and there's no trivial solution if label_parsed must be used (in my case, greek symbols). – J.Lai Dec 02 '15 at 13:51
  • yes, i think so. Label_parsed is used to decrypt functions like expression(). so, somewhere, you have to "encrypt" your labels. But did you get what you needed when removing labeller and keeping the face parameter in theme? – bhuss Dec 02 '15 at 14:14