How to use italic in label_bquote for strip text lables

天大地大妈咪最大 提交于 2021-02-11 14:29:58

问题


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?

来源:https://stackoverflow.com/questions/59954819/how-to-use-italic-in-label-bquote-for-strip-text-lables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!