Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

R ggplot2 using italics and non-italics in the same category label

$
0
0

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)


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>