本文介绍了通过geom_text()传递对象的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图产生一个图,我想大胆地使用 geom_text()的一部分字符串。以下是一些示例数据和图表: pre $ temp text = c(文本的小圆点), 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) 理想情况下,我想要做两件事。首先,最重要的是我想鼓励子弹这个词并且仅仅是那个词。其次,如果可能的话,我想将单词项目符号的颜色更改为红色,以匹配单词大的颜色。 我读过 添加构面 肯定有一个更容易的方法来面对da作为矢量,但这是我在星期五下午提出的。 :-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 of text))),简化= TRUE) dat $ a2 = sapply(as.character(dat $ a), function(x)deparse(bquote(small〜phantom(bold(。(x))) ), simplified = 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) 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 1Matrix products: defaultattached base packages:[1] stats graphics grDevices utils datasets methods baseother 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.5loaded 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 解决方案 Working with plotmath in ggplot2 confounds me every time, especially when trying to pass in a variable.I think I got things to work how you want them, complete with one bold word with a separate color passed from a variable. It involves deparse() and bquote() and bold() along with phantom(). Phew.The phantom() holds the place of the words you don't want to show with the specific color, so it takes two annotate() layers to make the whole expression using different colors.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)Adding facetsI feel certain there is an easier approach to facets and "a" as a vector but this is what I've come up with on a Friday afternoon. :-DMy approach is to loop through the (character) vector and make a new variable with all the deparsing and bquoting and such. The faceting variable is part of the text dataset.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)Working with multiple variablesLast, if you need to get characters pasted together with colors and font style and such using multiple columns in a vector you an do so. My approach involves loping through every row in the dataset, working with the columns that contain the label info. Note I'm working with characters (not factors) to avoid problems.I pull in the helper function pmap() from package purrr for rowwise work.If colors need to be different for the highlighted word in each facet you can define the colors in the data.frame and use scale_color_identity to use those colors.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() 这篇关于通过geom_text()传递对象的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 21:07