Using italics and non-italics in the same category label
Asked Answered
M

2

6

For my ggplot figure, I want to label categories on a barplot with the first word being italicized, while the following words are non-italicized. I want the category labels to look like follows:

Staphylococcacae (OTU 1)

Streptococcus (OTU 300)

I've found examples using expression() where I can italicize a few category labels, but I would like to be able to do this for many different categories.

The code to make a plot is as follows (but my data has many more bars to plot).

library(ggplot2)

data <- data.frame(
  bactname = c("Staphylococcaceae", "Moraxella", "Streptococcus", "Acinetobacter"),
  OTUname = c("OTU_1", "OTU_2", "OTU_3", "OTU_4"),
  value = c(-0.5, 0.5, 2, 3)
)

data$name <- paste0(
  data$bactname, " (", data$OTUname, ")"
)
data$name <- factor(
  data$name,
  levels = data$name[order(data$value)], ordered = TRUE
)

ggplot(data, aes(name, value)) + 
  geom_col() + coord_flip()

Created on 2020-01-28 by the reprex package (v0.3.0)

Miliary answered 1/9, 2016 at 23:30 Comment(0)
R
3

You can make a vector of expressions, and apply it to the labels argument in scale_x_discrete:

labs <- sapply(
  strsplit(as.character(data$name), " "), 
  function(x) parse(text = paste0("italic('", x[1], "')~", x[2]))
)

ggplot(data, aes(name, value)) + 
  geom_col() + coord_flip() +
  scale_x_discrete(labels = labs)

If you have spaces in your labels e.g. OTU 100, you may want to substitute a tilde for the space, e.g. OTU~100.

Redblooded answered 2/9, 2016 at 0:11 Comment(0)
B
8

I would use the glue and ggtext packages.

library(tidyverse)
library(ggtext)
library(glue)

data <- data.frame(
  bactname = c("Staphylococcaceae", "Moraxella", "Streptococcus", "Acinetobacter"),
  OTUname = c("OTU_1", "OTU_2", "OTU_3", "OTU_4"),
  value = c(-0.5, 0.5, 2, 3)
)

data %>% mutate(
  name = glue("*{bactname}* ({OTUname})"),
  name = fct_reorder(name, value)
) %>%
  ggplot(aes(name, value)) + 
  geom_col() + coord_flip() +
  theme(axis.text.y = element_markdown())

Created on 2020-01-29 by the reprex package (v0.3.0)

Barnhill answered 29/1, 2020 at 17:3 Comment(0)
R
3

You can make a vector of expressions, and apply it to the labels argument in scale_x_discrete:

labs <- sapply(
  strsplit(as.character(data$name), " "), 
  function(x) parse(text = paste0("italic('", x[1], "')~", x[2]))
)

ggplot(data, aes(name, value)) + 
  geom_col() + coord_flip() +
  scale_x_discrete(labels = labs)

If you have spaces in your labels e.g. OTU 100, you may want to substitute a tilde for the space, e.g. OTU~100.

Redblooded answered 2/9, 2016 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.