1

I would like to italicize a part of a term in axis text (not title) in R ggplot2.

I have some bacterial species names that I should write in italic and besides I have the strain name that should be in plain text.

Here is an example of what I have:

My data frame looks like this

 MyDF <- data.frame(Activity=rep(c("Activity 1", "Activity 2", "Activity 3"), each = 6), 
                   Bacteria = rep(c("Escherichia coli Strain 1", "Escherichia coli Strain 2",
                                "Escherichia coli Strain 3", "Escherichia coli Strain 4",
                                "Escherichia coli Strain 5", "Escherichia coli Strain 6"), each=3),
                   Facet=rep(c("Facet 1", "Facet 1","Facet 2","Facet 2","Facet 2","Facet 2"), each=3),
                   Production=rep(1:18))
MyDF
Activity                  Bacteria   Facet Production
1  Activity 1 Escherichia coli Strain 1 Facet 1          1
2  Activity 1 Escherichia coli Strain 1 Facet 1          2
3  Activity 1 Escherichia coli Strain 1 Facet 1          3
4  Activity 1 Escherichia coli Strain 2 Facet 1          4
5  Activity 1 Escherichia coli Strain 2 Facet 1          5
6  Activity 1 Escherichia coli Strain 2 Facet 1          6
7  Activity 2 Escherichia coli Strain 3 Facet 2          7
8  Activity 2 Escherichia coli Strain 3 Facet 2          8
9  Activity 2 Escherichia coli Strain 3 Facet 2          9
10 Activity 2 Escherichia coli Strain 4 Facet 2         10
11 Activity 2 Escherichia coli Strain 4 Facet 2         11
12 Activity 2 Escherichia coli Strain 4 Facet 2         12
13 Activity 3 Escherichia coli Strain 5 Facet 2         13
14 Activity 3 Escherichia coli Strain 5 Facet 2         14
15 Activity 3 Escherichia coli Strain 5 Facet 2         15
16 Activity 3 Escherichia coli Strain 6 Facet 2         16
17 Activity 3 Escherichia coli Strain 6 Facet 2         17
18 Activity 3 Escherichia coli Strain 6 Facet 2         18

And the code used to generate the plot is:

MyPlot<- ggplot(MyDF, aes(x=Bacteria, y=Production, fill= Activity)) + 
  geom_bar(stat="identity", 
           position=position_dodge()) +
  facet_grid(Activity ~ Facet, drop=FALSE, scales="free", space = "free_x")+
  theme(axis.text.x = element_text(angle = 55, hjust = 1))


MyPlot
Najoua
  • 81
  • 5
  • Does this answer your question? [How to format selected axis text words with italic and bold using ggtext](https://stackoverflow.com/questions/72027474/how-to-format-selected-axis-text-words-with-italic-and-bold-using-ggtext) – starja Oct 13 '22 at 15:50
  • Thank you @starja, but no. When it moves to the next facet it starts over. – Najoua Oct 13 '22 at 20:40

2 Answers2

3

use the ggtext package is the method I use, although there may be others. You can then add some markdown or html-style formatting to text strings:

library(ggtext)

    MyDF$Bacteria_ital <-gsub("(.*)( Strain.*)","<i>\\1</i>\\2" , MyDF$Bacteria)


ggplot(MyDF, aes(x=Bacteria_ital, y=Production, fill= Activity)) + 
  geom_bar(stat="identity", 
           position=position_dodge()) +
  facet_grid(Activity ~ Facet, drop=FALSE, scales="free", space = "free_x")+
  theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
  theme(axis.text.x = ggtext::element_markdown())
Matt L.
  • 2,753
  • 13
  • 22
  • Thank you @Matt but can you help me decipher the second line ? – Najoua Oct 13 '22 at 20:41
  • `` is the html italics code. this line just adds that in the proper place in your string, and before the "Strain" text using regex (type `?regex()` for help in R on this). `` marks the end of the italics. – Matt L. Oct 14 '22 at 14:41
  • alternatively you can just construct this yourself when you enter data so that your names read "Bacteria name Strain X" – Matt L. Oct 14 '22 at 14:42
  • Thank you for this explanation, however it doesn't work on my dataset. I don't get the meaning of \\1 and \\2. – Najoua Oct 19 '22 at 18:12
  • If your bacteria name pattern is different than your example here, you'll need to explore the regex help to adapt the matching pattern. the \\1 and \\2 reference the groups matched in the first () and second () positions of regex, in order to insert the italics html code s`` in the correct spot.s – Matt L. Oct 20 '22 at 00:59
  • Thnaks a lot, now it works. – Najoua Oct 20 '22 at 08:40
1

Alternative:

library(glue)
library(ggtext)
library(tidyverse)
MyDF1 <- MyDF %>% 
  mutate(Bacteria_name = word(Bacteria, 1,2, sep=" "),
         Strain_name = word(Bacteria, -2,-1, sep=" ")) %>% 
  mutate(Bacteria_new = glue("<i>{Bacteria_name }</i>{Strain_name}")) %>% 
  ggplot(aes(x=Bacteria_new, y=Production, fill= Activity)) + 
  geom_bar(stat="identity", 
           position=position_dodge()) +
  facet_grid(Activity ~ Facet, drop=FALSE, scales="free", space = "free_x")+
  theme(axis.text.x =  element_markdown(angle = 55, size = 18, hjust =1))
  )
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you @TarJae. I belive that neither the names of my bacteria nor those of my strains should have space. What if the names contain spaces. Could you also help me decipher the first and seconf mutate ? – Najoua Oct 13 '22 at 20:48