How to italicize part (one or two words) of an axis title

前端 未结 3 651
情深已故
情深已故 2020-12-03 09:31

Is there any way to change the style of part of an axis title while keep the rest part unchanged? In my case, How could I italicize
\"bacteria X\" in the y-axis title? T

3条回答
  •  半阙折子戏
    2020-12-03 10:07

    This can be achieved using element_markdown() from the ggtext package.

    ggplot(fig1, aes(cf, Freq, fill = Var1)) +
      geom_bar(stat = "identity") +
      labs(
        x = "Groups",
        y = "No. of *bacteria X* isolates with corresponding types",
        fill = "Var1"
      ) +
      theme(axis.title.y = ggtext::element_markdown())
    

    Notice the * around bacteria X in the y axis title. Setting axis.title.y to element_markdown has the effect that the axis title is rendered as markdown. Thus, text inside * will be displayed in italics.

    An even easier solution is using the mdthemes package which provides themes that interpret text as makrdown out of the box, i.e. no need to call theme. Here's an example.

    ggplot(mtcars, aes(hp, mpg)) +
      geom_point() +
      mdthemes::md_theme_classic() +
      labs(title = "**Bold Title**", x = "*Italics axis label*")
    

提交回复
热议问题