Home > database >  How to italicize some words in a sentence in x axis text in ggplot facet
How to italicize some words in a sentence in x axis text in ggplot facet

Time:10-14

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

CodePudding user response:

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())

CodePudding user response:

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))
  )
  • Related