首页 > 解决方案 > 通过 geom_text() 传递的对象的加粗子字符串

问题描述

我正在尝试生成一个图表,我想将 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)

在此处输入图像描述

理想情况下,我想做两件事。首先,最重要的是我想大胆地使用“子弹”这个词,而且只有那个词。其次,如果可能的话,我想将项目符号的颜色更改为“红色”,以匹配“大”这个词的颜色。

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

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

此外,我希望能够从环境中调用一个对象,以便我可以将其打包为一个函数。我已经阅读了关于bquote() 在这里使用的信息,但显然我无法测试它,因为我无法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 

标签: htmlrggplot2fonts

解决方案


在 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)

在此处输入图像描述

添加构面

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

我的方法是遍历(字符)向量,并使用所有 deparsing 和 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)

在此处输入图像描述

使用多个变量

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

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()

在此处输入图像描述


推荐阅读