在注释中使用不同的字体样式(ggplot2) [英] Using different font styles in annotate (ggplot2)

查看:52
本文介绍了在注释中使用不同的字体样式(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码生成一个带有一些注释的简单图表:

I'm using the code below to generate a simple chart with some annotations:

require(ggplot2); data(mtcars)
ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  annotate("text", x = 4, y = 25, label = "This should be bold\nand this not",
           colour = "red") +
  geom_vline(xintercept = 3.2, colour = "red")

在该图表上,我想将粗体应用于文本注释中短语的第一部分:

On that chart I would like to apply bold font to the first part of the phrase in the text annotation:

这应该是粗体

但我希望文本的其余部分在字体和样式方面保持不变.

but the I wish for the remaining part of the text to remain unaltered with respect to the font face and style.

推荐答案

如何在 parse = TRUE 中使用 plotmath 语法:

How about using plotmath syntax with parse = TRUE:

ggplot(mtcars, aes(x = wt, y = mpg)) + 
    geom_point() +
    annotate("text", x = 4, y = 25, 
            label = 'atop(bold("This should be bold"),"this should not")',
            colour = "red", parse = TRUE) +
    geom_vline(xintercept = 3.2, colour = "red")

这篇关于在注释中使用不同的字体样式(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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