在注释中使用不同的字体样式(ggplot2)

时间:2015-07-22 16:08:21

标签: r text charts ggplot2 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")

Simple plot

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

  

这应该是粗体

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

2 个答案:

答案 0 :(得分:27)

如果您在两个注释中将其拆分没有问题,您可以这样做:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
   <Entry>
      <CName>CCXXX</CName>
      <CCH_Summary>Marketing</CCH_Summary>
      <CCH_Rollup_1>Marketing Group</CCH_Rollup_2>
      <CCH_Rollup_2>Marketing (COR)</CCH_Rollup_2>
      <CCH_Rollup_3>Cost of Revenue</CCH_Rollup_3>
      <CCH_Rollup_4>Total Cost Centre</CCH_Rollup_4>
   </Entry>
   <Entry>
      <CName>CCXXX</CName>
      <CCH_Summary>Finance</CCH_Summary>
      <CCH_Rollup_1>Finance Ret</CCH_Rollup_2>
      <CCH_Rollup_2>Finance Acct (COR)</CCH_Rollup_2>
      <CCH_Rollup_3>Cost of Revenue</CCH_Rollup_3>
      <CCH_Rollup_4>Total Cost Centre</CCH_Rollup_4>
   </Entry>
</Data>

答案 1 :(得分:20)

如何将 plotmath 语法与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")

enter image description here