I'm trying to produce a graph and I'd like to bold part of a string of a geom_text()
. Here's some sample data and a graph:
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)
There are two thigns I'd like to do, ideally. First, the most important is that I'd like to embolden the word "bullet" and only that word. Second, if possible I'd like to change the color of the word bullet to "red" , to match that of the word "big".
I've read on here solutions involving the bold()
function which seems to come from the grDevices
package but I can't get that to work. It gives the following error when I try the example in that link:
> paste("No. of ", bold("bacteria X"), " isolates with corresponding types")
Error in bold("bacteria X") : could not find function "bold"
Further, I'd like to be able to call an object from the environment so that I can package this up as a function. I've read about using bquote()
here, but obviously I haven't been able to test that since I can't get bold()
to work.
So, 1) Why can't I get bold()
to work? 2) Is bquote()
with bold()
the best way to do this? 3) I'd imagine html tags may be an easy way to do this, is there any way to pass a string containing html tags and have ggplot interpert it properly? 4) I don't expect much on the color front as the bold part is what I'm really concerned about, but if you have any suggestions I'll gladly take them. Thanks!
> 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