0

There have been many proposed solutions to many slightly different problems, but none quite catches my specific problem.

What I want is the following: I want to mix normal subfig labels (a, b, c, ...) with species names in italics. So far, I only want do this for four species, so I could live with a manual solution. But a custom function automatizing the process would be neat...

E.g. the strip text of species a would be:

a) Genus species

Here is a reproducible example with the iris dataset:

library(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
p + facet_grid(. ~ Species)

So I would like the strip text to read (ignoring the missing Iris for now...):

a) setosa

b) versicolor

c) virginica

I tried to add the following modifications:

p + facet_grid(. ~ Species, labeller = label_bquote(cols = letters[1:4] ~ .(Species)))
p + facet_grid(. ~ Species, labeller = label_bquote(cols = letters[1:4] ~ italic(Species)))
p + facet_grid(. ~ Species, labeller = label_bquote(cols = letters[1:4] ~ .(italic(Species))))

Adding letters[1:4] obviously doesn't work. I also don't get, why italic(Species) would give me 'Species' (meaning literally the word Species in italics) while .(Species) would actually paste the numerical levels of Species (1, 2, 3). And .(italic(Species)) just gives me an error message saying could not find function "italic".

How can I get the levels of Species pasted in italics? And how would I add then the letters to it?

EDIT:

I actually used the proposed solution by Axeman, that is label_bquote(cols = .(letters[Species]) ~ italic(.(as.character(Species))))

Here is the example again with the code that ran until recently:

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
p + facet_grid(. ~ Species, labeller = label_bquote(cols = .(letters[Species]) ~ italic(.(as.character(Species)))))

However, ggplot2 or some other dependency seem to have changed so that this does not work anymore. Now it gives me the error Error in letters[Species] : could not find function "[". I have no clue why! Any idea?

bamphe
  • 328
  • 3
  • 12
  • You can find a solution here: https://stackoverflow.com/a/20037877/9022665 – Jonathan V. Solórzano Jan 28 '20 at 19:16
  • Thanks for your suggestion. Maybe I wasn't clear enought, I just want to have the species name in italics but the letter in front in normal font. If I set the face in theme, the whole strip text will be in italics... – bamphe Jan 28 '20 at 19:29
  • Ok, then this might be helpful: https://stackoverflow.com/a/34980273/9022665 – Jonathan V. Solórzano Jan 28 '20 at 20:02
  • 1
    It would be `italic(.(as.character(Species))))`. Not sure about the letters. You might just have write out the factor levels and use `label_parsed`. E.g. use `iris$facet <- c('"a)" ~ italic(setosa)', '"b)" ~ italic(versicolor)', '"c)" ~ italic(virginica)')` and then `facet_grid(~ facet, labeller = label_parsed)`. – Axeman Jan 29 '20 at 02:07
  • 2
    Actually, `label_bquote(cols = .(letters[Species]) ~ italic(.(as.character(Species))))` works. – Axeman Jan 29 '20 at 02:09
  • Thanks a lot, Axeman! If you post this as an answer I would accept it as such ;) – bamphe Jan 29 '20 at 08:59
  • I still have a really hard time understanding the underlying principles of plotmath and especially the labeller function. I just don't get it... – bamphe Jan 29 '20 at 09:01

0 Answers0