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

后端 未结 2 1640
醉梦人生
醉梦人生 2020-12-20 23:46

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

2条回答
  •  失恋的感觉
    2020-12-20 23:54

    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)

提交回复
热议问题