如何在ggplot2中的图例中斜体显示一个类别 [英] How to italicize one category in a legend in ggplot2

查看:341
本文介绍了如何在ggplot2中的图例中斜体显示一个类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我在ggplot2中的图例,我有2个类别,我该如何仅将1个类别用斜体表示,而将其他类别用斜体表示呢?

For my legend in ggplot2 I have 2 categories, how do I italicize only 1 category and not the other?

作为示例,请考虑以下图表.如何设置斜体的手册"?

As an example, consider the following plot. How can I set "Manual" in italics?

library(ggplot2)

ggplot(data = mtcars, aes(x = as.factor(am), fill = as.factor(am))) + 
  geom_bar() + 
  scale_fill_discrete(
    "Transmission",
    breaks = c(0, 1),
    labels = c("Automatic", "Manual")
  )

reprex软件包(v0.3.0)创建于2020-01-01 sup>

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

推荐答案

您可以使用expressionitalic在标签上创建斜体文本.

You can use expression and italic to create italicized text on labels.

library(ggplot2)

ggplot(data = mtcars, aes(x = as.factor(am), fill = as.factor(am))) + 
  geom_bar() + 
  scale_fill_discrete(
    "Transmission",
    breaks = c(0, 1),
    labels = c("Automatic", expression(italic("Manual")))
  )

reprex软件包(v0.3.0)创建于2020-01-01 sup>

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

这篇关于如何在ggplot2中的图例中斜体显示一个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆