74

Is there any way to change the style of part of an axis title while keep the rest part unchanged? In my case, How could I italicize
"bacteria X" in the y-axis title? To my knowledge, the command theme(axis.title.y=element_text(face="italic")) can only change the whole y-aixs title, is it?

ggplot(fig1,aes(x=cf,y=Freq,fill=Var1)) +
geom_bar(stat="identity") +
labs(x="Groups",y="No. of bacteria X isolates with corresponding types",fill="Var1") +
theme(axis.title.y=element_text(face="italic"))
zx8754
  • 52,746
  • 12
  • 114
  • 209
eze
  • 741
  • 1
  • 5
  • 3

3 Answers3

72

You could make an expression like this:

my_y_title <- expression(paste("No. of ", italic("bacteria X"), " isolates with corresponding types"))
.... + labs(y=my_y_title)
Heroka
  • 12,889
  • 1
  • 28
  • 38
  • 5
    How does this work when part of the title text (inside paste) comes from a variable in the workspace? I noticed that when I bring other variables into the paste, then "paste" is interpreted literally and the title starts with "paste(Chart title..." – Bobby May 22 '18 at 12:32
  • 1
    Exactly, our problem too. I've put a suggestion on the `ggplot` GitHub page: https://github.com/tidyverse/ggplot2/issues/2743 – MS Berends Jul 09 '18 at 13:02
  • 2
    @Bobby `word <- "the word in italic"` followed by `bquote('Example map with'~italic(.(word)))` found at https://stackoverflow.com/questions/31927984/using-italic-with-a-variable-in-ggplot2-title-expression#31928067 – RFelber Nov 30 '18 at 10:36
  • 4
    Calling this variable within `ggtitle()` also will not interpret this correctly. Using in `bquote(custom_title)`, `bquote(.(custom_title))`, or `bquote(~.(custom_title))` do not produce anything close to the desired result. This answer may not be up-to-date and should be edited to provide an example of what you mention, RFelber. – GenesRus Feb 21 '20 at 00:56
32

This can be achieved using element_markdown() from the ggtext package.

ggplot(fig1, aes(cf, Freq, fill = Var1)) +
  geom_bar(stat = "identity") +
  labs(
    x = "Groups",
    y = "No. of *bacteria X* isolates with corresponding types",
    fill = "Var1"
  ) +
  theme(axis.title.y = ggtext::element_markdown())

Notice the * around bacteria X in the y axis title. Setting axis.title.y to element_markdown has the effect that the axis title is rendered as markdown. Thus, text inside * will be displayed in italics.

An even easier solution is using the mdthemes package which provides themes that interpret text as makrdown out of the box, i.e. no need to call theme. Here's an example.

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  mdthemes::md_theme_classic() +
  labs(title = "**Bold Title**", x = "*Italics axis label*")

enter image description here

Thomas Neitmann
  • 2,552
  • 1
  • 16
  • 31
  • Is there a way to use mdthemes to allow use of markdown in the titles without changing the theme of the entire graph? – Susie Derkins Jan 24 '22 at 17:01
  • Please take a look at the [{ggtext}](https://github.com/wilkelab/ggtext) package for this kind of functionality. Essentially you have to use `ggtext::element_markdown()` rather than `element_text()` inside `theme()`. – Thomas Neitmann Jan 27 '22 at 17:32
  • 1
    Might be pure coincidence, but it seems as if the author of the mdthemes package (which seems to be awesome) has the same name as you. If it **is** you, a disclaimer is generally considered good practice. – tjebo Apr 14 '22 at 12:14
  • @ThomasNeitmann Surprisingly, I am unable to italicize using the ggtext::element_markdown() approach, the asterisks don't get rendered but the text isn't italic. Subscript, etc. using ... works. Did anyone else encounter that issue? UPDATE: Issue has already been reported and will be fixed in the next version: https://github.com/wilkelab/ggtext/issues/83 – Felix May 29 '22 at 17:18
18

I believe RFelber's suggestion is what you are after. Try this:

labs(x="Groups",
     y=expression('No. of'~italic(bacteria X)~'isolates with corresponding types'),
     fill="Var1")

I did not need to use the bquote() function. The tildes produce single spaces for terms that are outside of the quotes.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
TCS
  • 451
  • 6
  • 18
  • 1
    Thanks - that's a whole lot simpler than using paste or bquote. Whereabouts it the documentation that explains that the tilde works like that? – smartse Aug 29 '19 at 14:45
  • What can do you if you don't want spaces where the tildes are? – GenesRus Feb 21 '20 at 00:57
  • Use the paste answer? – stephanmg Jul 21 '20 at 07:26
  • I needed to add backticks to my code. I can't show it exactly here since backticks are the markdown symbol for code on SO and I'm not sure how to escape it for my code, so I'll paste the code plainly: expression('Number of Days in Temperature Range of'~italic(`Phytophthora infestans`)~'in Halifax, VA.') – John Polo Aug 26 '22 at 15:51