通过 geom_text() 传递的对象的加粗子串

cpa*_*ong 1 html fonts r ggplot2

我正在尝试生成一个图表,并且我想将 .a 字符串的一部分加粗geom_text()。这是一些示例数据和图表:

temp <- data.frame(num = c("big"),
                   text = c("small bullet point of text"),
                   col = c("red"))

ggplot(data = temp) +
  lims(x = c(0,100), y = c(0, 100)) + 
  geom_text(aes(x = 20, y = 50, label = num, color = col), size = 25) +
  geom_text(aes(x = 60, y = 50, label = text), size = 3)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

理想情况下,我想做两件事。首先,最重要的是,我想给“子弹”这个词加气,而且只用那个词。其次,如果可能的话,我想将 Bullet 一词的颜色更改为 "red" ,以匹配 "big" 一词的颜色。

我在这里阅读了涉及bold()似乎来自grDevices包的功能的解决方案,但我无法让它工作。当我尝试该链接中的示例时,它会出现以下错误:

> paste("No. of ", bold("bacteria X"), " isolates with corresponding types")
Error in bold("bacteria X") : could not find function "bold"
Run Code Online (Sandbox Code Playgroud)

此外,我希望能够从环境中调用一个对象,以便我可以将其打包为一个函数。我已经阅读了有关使用bquote() here 的信息,但显然我无法对其进行测试,因为我无法开始bold()工作。

所以,1)为什么我bold()不能上班?2)bquote()bold()最好的方法来做到这一点?3)我想html标签可能是一种简单的方法来做到这一点,有没有办法传递包含html标签的字符串并让ggplot正确解释它?4)我对颜色的期望不高,因为粗体部分是我真正关心的,但如果你有任何建议,我很乐意采纳。谢谢!

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2008 R2 x64 (build 7601) Service Pack 1

Matrix products: default 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tidyr_0.8.1   stringr_1.3.1 scales_0.5.0  readxl_1.1.0  ggplot2_2.2.1 dplyr_0.7.5  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17     knitr_1.17       bindr_0.1.1      magrittr_1.5     tidyselect_0.2.3
 [6] munsell_0.4.3    colorspace_1.3-2 R6_2.2.2         rlang_0.2.1      plyr_1.8.4      
[11] tools_3.4.1      grid_3.4.1       gtable_0.2.0     digest_0.6.12    lazyeval_0.2.0  
[16] assertthat_0.2.0 tibble_1.4.2     bindrcpp_0.2.2   reshape2_1.4.2   purrr_0.2.4     
[21] glue_1.2.0       labeling_0.3     stringi_1.1.7    compiler_3.4.1   pillar_1.1.0    
[26] cellranger_1.1.0 pkgconfig_2.0.1 
Run Code Online (Sandbox Code Playgroud)

aos*_*ith 5

在 ggplot2 中使用plotmath每次都让我感到困惑,尤其是在尝试传入变量时。

我让事情按照你想要的方式工作,用一个粗体字完成,从变量传递一个单独的颜色。它涉及deparse() bquote() bold()沿phantom()。呼。

phantom()持有的你的话的地方希望与特定的颜色来表示,所以需要两个annotate()层,使采用不同颜色的整体表现。

a = "bullet"
ggplot(mtcars, aes(x = wt, y = mpg)) + 
    annotate("text", x = 4, y = 25, 
             label = deparse(bquote(phantom(small)~bold(.(a))~phantom(point~of~text))),
             colour = "red", parse = TRUE) +
    annotate("text", x = 4, y = 25, 
             label = deparse(bquote(small~phantom(bold(.(a)))~point~of~text)),
             colour = "black", parse = TRUE)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

添加构面

我确信有一种更简单的方法来处理刻面和“a”作为向量,但这是我在周五下午提出的。:-D

我的方法是遍历(字符)向量并使用所有解析和 bquoting 等创建一个新变量。faceting 变量是文本数据集的一部分。

dat = data.frame(x = c(4, 4),
                 y = c(25, 25),
                 a = c("bullet", "shell"),
                 am = c(0, 1) )

dat$a1 = sapply(as.character(dat$a), 
                       function(x) deparse(bquote(phantom(small)~bold(.(x))~phantom(point~of~text))), 
                simplify = TRUE)
dat$a2 = sapply(as.character(dat$a), 
                       function(x) deparse(bquote(small~phantom(bold(.(x)))~point~of~text)), 
                simplify = TRUE)

ggplot(mtcars, aes(x = wt, y = mpg) ) +
    geom_text(data = dat, aes(x, y, label = a1), color = "red", parse = TRUE) +
    geom_text(data = dat, aes(x, y, label = a2), color = "black", parse = TRUE) +
    facet_wrap(~am)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

使用多个变量

最后,如果您需要使用矢量中的多列将字符与颜色和字体样式粘贴在一起,您可以这样做。我的方法涉及遍历数据集中的每一行,使用包含标签信息的列。注意我正在使用字符(而不是因素)来避免出现问题。

pmap()从包purrr中提取辅助函数进行行式工作。

如果每个方面中突出显示的单词的颜色需要不同,您可以在 data.frame 中定义颜色并scale_color_identity使用这些颜色。

dat = data.frame(x = c(4, 4),
                 y = c(25, 25),
                 a = c("bullet", "shells"),
                 b = c("point of text", "on the beach"),
                 am = c(0, 1),
                 color = c("red", "purple"),
                 stringsAsFactors = FALSE)

library(ggplot2)
library(purrr)

dat$a1 = pmap_chr(dat, function(a, b, ...) 
                deparse(bquote(phantom(small)~bold(.(a))~phantom(.(b))) ) )

dat$a2 = pmap_chr(dat, function(a, b, ...) 
     deparse(bquote(small~phantom(bold(.(a)))~.(b))))

ggplot(mtcars, aes(x = wt, y = mpg) ) +
     geom_text(data = dat, aes(x, y, label = a1, color = color), parse = TRUE) +
     geom_text(data = dat, aes(x, y, label = a2), color = "black", parse = TRUE) +
     facet_wrap(~am) +
     scale_color_identity()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明