本文介绍了图标题科学名称+符号textGrob gtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 首先,我要感谢Baptiste先生帮助我改进我的R脚本,使用gtable / textGrob在左下角的左下角添加一个标题,如下所示: pre code library(grid) library(gridExtra) library(ggplot2) p1 g1 g2 g g g grid.newpage() grid.draw(g) 然而,我想再添加两件事: - 例如,根据上面提到的标题,我只想简化is这个单词,而其余的都是纯文本的。 (2 )我还会在标题中添加符号,例如点形状= c(1,22);颜色= c(黑色,红色); fill = c(red,black)。 我该怎么做?我是R程序的新手用户,因此非常感谢您的帮助。 更新: 我已经在@Docconcoct,@ user20650的帮助下解决了查询1和@baptiste使用这个脚本: library(grid) library(gridExtra) library(ggplot2 ) g1 g2 g 字幕< - textGrob(表达式(paste(图1.这个,italic(是),一个标题)),hjust = 0,x = 0)g g grid.newpage() grid.draw(g) 对于查询2,正如@baptiste爵士所说,在我给他的原始电子邮件中,我已经有了关于合并情节的图例。然而,在图标题中,我需要说明图例中的那些符号以及情节的一些其他细节。根据Sir baptiste给出的例子,我需要包括supp的含义以及标题中OJ(黑色圆圈)和VC(黑色三角形)的符号。 再次,非常感谢!解决方案将依赖于LaTeX或类似的文本渲染,特别是线条缠绕的棘手问题,但是可以在R级别设计一些内容以便于包含与给定图形对应的绘图符号。在这些方面, gl = extract_legend_grobs(p) caption = caption_plot(图1.我们指的是到点{{gl $ points [supp == OG'']}}。理论模型显示为{{gl $ lines [type =='theory']}}。,gl) print(caption,output =latex) ##图1.我们指的是点\includegraphics {gl_p_1.png}。 ##理论模型显示为\includegraphics {gl_l_1.png}。 有趣的想法,但可能需要很多工作才能完成。 也可以设计出一个快速而脏的R图形输出,但是将字幕作为图形的一部分是不常见的(而R图形对文本来说不是特别好)。 > 这是一个很弱的尝试,使标题混合符号和文本。理想情况下,文本将首先被分割成单独的单词(为换行提供更多选项),但绘图表达式会使其不方便。 下一步是增加一些便捷的包装来生成通用符号,并交织两个grobs列表。 library(grid) library(gridExtra) inwidth< - function x,margin = unit(1,mm)){ if(inherits(x,text)) convertWidth(grobWidth(x)+ margin,in,valueOnly = TRUE)否则 convertWidth(单元(1,line)+ margin,in,valueOnly = TRUE)} captionGrob< - function(...,width = unit(4,in),debug = FALSE){ maxw lg lw stopifnot(all(lw #find breaks cw< - cumsum(lw) bks #行列表 tg 开始结束 for(line in seq_along(starts)){ ids< - seq(starts [line],ends [line]) sumw< - do.call(sum,lw [ids]) neww 填充符< - rectGr ob(gp = gpar(col = NA,fill = NA), width = unit(neww,in), height = unit(1,line)) grobs < -c(lg [ids],列表(填充符)) #存储当前行 tg [[line]]< - arrangeGrob(grobs = grobs,nrow = 1, widths = unit(c(lw [ids],neww),in)) } #排列一列中的所有行 grid.arrange(grobs = tg,ncol = 1, heights = unit(rep(1,length(tg)),line)) if(debug)grid。 rect(width = width,gp = gpar(fill = NA,lty = 2))} tg textGrob) pGrob ,重要,,但是,和 fill,size = 1,...){ rectGrob(...,width = unit(size,line),height = unit(size,line),gp = gpar(fill = fill ))} pg grid.newpage() captionGrob(tg [[1]],pg [[1]],pg [[2]],pg [[3]],tg [[2 ]],tg [[3]],pg [[4]],tg [[4]],pg [[5]],tg [[5]]) / pre> First of all I would like to thank Sir Baptiste for helping me improve my R script by adding a caption at the bottom left the of the combined plots using gtable/textGrob as shown below:library(grid)library(gridExtra)library(ggplot2)p1 <- p2 <- ggplot()g1 <- ggplotGrob(p1)g2 <- ggplotGrob(p2)g <- rbind(g1, g2)caption <- textGrob("Figure 1. This is a caption", hjust=0, x=0)g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))grid.newpage()grid.draw(g)However, I want to add two more things:(1) Insert a scientific name to the caption, which should be written in italics. - For example, based on the caption mentioned above, I want to italize only the word "is" while the rest are in plain text.(2) I will also add symbols in the caption, e.g. point shapes=c(1,22); colours=c("black", "red"); fill=c("red", "black").How am I going to do these? I am a novice user of R program, hence your help is much appreciated. Thank you.UPDATE:I have already addressed query 1 with the help of @Docconcoct, @user20650 and @baptiste using this script:library(grid)library(gridExtra)library(ggplot2)g1 <- ggplotGrob(pl)g2 <- ggplotGrob(pl1)g <- rbind(g1, g2)caption <- textGrob(expression(paste("Figure 1. This", italic(" is"), " a caption")), hjust=0, x=0)g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))grid.newpage()grid.draw(g)For query 2, as stated by Sir @baptiste, in my original email to him, I already have a legend on the combined plots. However, in the figure caption, I need to state what are those symbols in the legend mean, and some other details of the plot. Based on the example given by Sir baptiste, I need to include what supp means, as well as the symbols of OJ (dark circle) and VC (dark triangle) in the caption.Again, many thanks! 解决方案 I think a good solution would rely on LaTeX or similar for the text rendering and particularly the tricky issue of line-wrapping, but something could be designed at R level to facilitate the inclusion of plotting symbols that correspond to a given graphic. Something along those lines,gl = extract_legend_grobs(p)caption = caption_plot("Figure 1. We are referring to the points {{gl$points[supp == OG'']}}. The theoretical model is shown as {{gl$lines[type == 'theory']}}.", gl)print(caption, output="latex")## "Figure 1. We are referring to the points \includegraphics{gl_p_1.png}.## The theoretical model is shown as \includegraphics{gl_l_1.png}."Interesting thought, but probably a lot of work to get it right.A quick-and-dirty R graphics output could also be devised, though it's uncommon to want captions to be part of the figure (and R graphics isn't particularly good with text).Here's a weak attempt at making a caption grob mixing symbols and text. Ideally the text would be split into individual words first (to offer more options for line breaks), but plotmath expressions make it inconvenient.Next step would be to add a few convenient wrappers to generate common symbols, and to interleave the two lists of grobs. library(grid)library(gridExtra)inwidth <- function(x, margin=unit(1,"mm")) { if(inherits(x, "text")) convertWidth(grobWidth(x)+margin, "in", valueOnly = TRUE) else convertWidth(unit(1,"line")+margin, "in", valueOnly = TRUE)}captionGrob <- function(..., width = unit(4, "in"), debug = FALSE){ maxw <- convertWidth(width, "in", valueOnly = TRUE) lg <- list(...) lw <- lapply(lg, inwidth) stopifnot(all(lw < maxw)) # find breaks cw <- cumsum(lw) bks <- which(c(0, diff(cw %% maxw)) < 0 ) # list of lines tg <- list() starts <- c(1, bks) ends <- c(bks -1, length(lg)) for(line in seq_along(starts)){ ids <- seq(starts[line], ends[line]) sumw <- do.call(sum,lw[ids]) neww <- maxw - sumw # missing width to fill filler <- rectGrob(gp=gpar(col=NA, fill=NA), width=unit(neww, "in"), height=unit(1, "line")) grobs <- c(lg[ids], list(filler)) # store current line tg[[line]] <- arrangeGrob(grobs=grobs, nrow = 1, widths = unit(c(lw[ids], neww), "in")) } # arrange all lines in one column grid.arrange(grobs=tg, ncol=1, heights = unit(rep(1, length(tg)), "line")) if(debug) grid.rect(width=width, gp=gpar(fill=NA, lty=2))}tg <- lapply(c(expression(bold(Figure~1.)~italic(Those)~points), "are important, ", "nonetheless", "and", "have value too."), textGrob)pGrob <- function(fill, size=1, ...){ rectGrob(..., width=unit(size,"line"), height=unit(size,"line"), gp=gpar(fill=fill))}pg <- mapply(pGrob, fill=1:5, size=0.5, SIMPLIFY = FALSE)grid.newpage()captionGrob(tg[[1]], pg[[1]], pg[[2]], pg[[3]], tg[[2]], tg[[3]], pg[[4]], tg[[4]], pg[[5]], tg[[5]]) 这篇关于图标题科学名称+符号textGrob gtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 03:48