本文介绍了导入图像的自定义图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我目前通过导入自定义图像并将它们用作geom_points在ggplot2中创建图块,类似于 #Paul Murrell的函数显示vp tree formatVPTree< - function(x,indent = 0){ end< - regexpr([)] +,?,x) sibling< - regexpr ,,x) child if((end lastchar cat(paste0(paste(rep(,indent ), substr(x,1,end - 1),\\\)) if(lastchar< nchar(x)){ formatVPTree (substring(x,lastchar + 1), indent - attr(end,match.length)+ 1)} } if(child> 0& amp; ;&(同胞 cat(paste0(paste(rep(,indent),collapse =), substr(x,1 ,孩子-3),\\\)) formatVPTree(substring(x,child + 1),indent + 1)} if(sibling> 0&& amp ;同胞< end&&&(child cat(paste0(paste(rep(,indent),collapse =), substr(x,1,sibling - 1),\\\)) formatVPTree(substring(x,sibling + 2),indent) } } I'm currently creating plots in ggplot2 by importing custom images and using them as geom_points, similar to this post, except I am looping through different images for unique levels of a factor.Is there an easy way to add these images to the legend? I've seen multiple posts on custom legends in ggplot2, but nothing that deals with imported images. 解决方案 I'm not sure how you will go about generating your plot, but this shows one method to replace a legend key with an image. It uses grid functions to locate the viewports containing the legend key grobs, and replaces one with the R logolibrary(png)library(ggplot2)library(grid)# Get imageimg <- readPNG(system.file("img", "Rlogo.png", package="png"))# Plotp = ggplot(mtcars, aes(mpg, disp, colour = factor(vs))) + geom_point() + theme(legend.key.size = unit(1, "cm"))# Get ggplot grobgt = ggplotGrob(p)grid.newpage()grid.draw(gt)# Find the viewport containing legend keyscurrent.vpTree() # not well formattedformatVPTree(current.vpTree()) # Better formatting - see below for the formatVPTree() function # Find the legend key viewports # The two viewports are: # key-4-1-1.5-2-5-2 # key-3-1-1.4-2-4-2# Or search using regular expressionsTree = as.character(current.vpTree())pos = gregexpr("\\[key.*?\\]", Tree)match = unlist(regmatches(Tree, pos))match = gsub("^\\[(key.*?)\\]$", "\\1", match) # remove square bracketsmatch = match[!grepl("bg", match)] # removes matches containing bg# Change one of the legend keys to the imagedownViewport(match[2])grid.rect(gp=gpar(col = NA, fill = "white"))grid.raster(img, interpolate=FALSE)upViewport(0)# Paul Murrell's function to display the vp treeformatVPTree <- function(x, indent=0) { end <- regexpr("[)]+,?", x) sibling <- regexpr(", ", x) child <- regexpr("[(]", x) if ((end < child || child < 0) && (end < sibling || sibling < 0)) { lastchar <- end + attr(end, "match.length") cat(paste0(paste(rep(" ", indent), collapse=""), substr(x, 1, end - 1), "\n")) if (lastchar < nchar(x)) { formatVPTree(substring(x, lastchar + 1), indent - attr(end, "match.length") + 1) } } if (child > 0 && (sibling < 0 || child < sibling)) { cat(paste0(paste(rep(" ", indent), collapse=""), substr(x, 1, child - 3), "\n")) formatVPTree(substring(x, child + 1), indent + 1) } if (sibling > 0 && sibling < end && (child < 0 || sibling < child)) { cat(paste0(paste(rep(" ", indent), collapse=""), substr(x, 1, sibling - 1), "\n")) formatVPTree(substring(x, sibling + 2), indent) }} 这篇关于导入图像的自定义图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 04:35