本文介绍了在ggplot2中用图中的图像标记图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以我有这个R脚本可以产生每个点标签的散点图。这样的: img1< - http://blog.gettyimages.com/wp-content/uploads/2013/ 01 / Siberian-Tiger-Running-Through-Snow-Tom-Brakefield-Getty-Images-200353826-001-628x419.jpg img2< - http://blog.gettyimages.com/wp-content/上传/ 2013/01 / Hurricane-Sandy-Andrew-Burton-Getty-Images-154986556.jpg imgdata colnames(imgdata)< - images txtdata< -data.frame (seq(1:10),seq(11:1),e,F,G,H,I,J)) 20),txtdata,imgdata) colnames(plotdata)< -c(var1,var2,texts,images) ggplot(data = plotdata,aes(plotdata [ ,1],plotdata [,2]))+ geom_point(data = plotdata,aes(plotdata [,1],plotdata [,2]))+ geom_text(aes(label = plotdata $点数,size = 2,hjust = 2)) 这给出了一个散点图,标记为A,B,C...等。 我想要做的是almos除了文本之外,我想用矢量或数据框链接中的图像(在本例中为imgdata)标记每个点。请注意,我仅以示例的方式选择了这些图像;我有更多的,所以我不能手动下载它们。解决方案您可以使用annotation_custom,但它会很多工作,因为每个图像必须呈现为栅格对象及其指定的位置。 library(ggplot2) library(png) library(grid) img1< - readPNG(c:/test/img1.png) g1 img2< - readPNG(c:/test/img2.png) g2< - rasterGrob(img2,interpolate = TRUE) plotdata< -data.frame(seq(1:2),seq(11:12)) ggplot(data = plotdata)+ scale_y_continuous(limits = c(0,4))+ scale_x_continuous(limits = c(0,4))+ geom_point(data = plotdata,aes(plotdata [,1],plotdata [,2]))+ annotation_custom(g1,xmin = 1, xmax = 1.5,ymin = 1,ymax = 1.5)+ annotation_custom(g2,xmin = 2,xmax = 2.5,ymin = 2,ymax = 2.5) pre> So I have this R script that can produce a scatter plot with labels of each point. Sth like this:img1<-"http://blog.gettyimages.com/wp-content/uploads/2013/01/Siberian-Tiger-Running-Through-Snow-Tom-Brakefield-Getty-Images-200353826-001-628x419.jpg"img2<-"http://blog.gettyimages.com/wp-content/uploads/2013/01/Hurricane-Sandy-Andrew-Burton-Getty-Images-154986556.jpg"imgdata<-data.frame(c(img1,img2,img1,img2,img1,img2,img1,img2,img1,img2))colnames(imgdata)<-"images"txtdata<-data.frame(c("A","B","C","D","E","F","G","H","I","J"))plotdata<-data.frame(seq(1:10),seq(11:20),txtdata,imgdata)colnames(plotdata)<-c("var1","var2","texts","images")ggplot(data=plotdata, aes(plotdata[,1],plotdata[,2])) + geom_point(data=plotdata, aes(plotdata[,1],plotdata[,2])) + geom_text(aes(label=plotdata$points,size=2, hjust=2))This gives a scatter plot, where each point is labelled as "A", "B", "C"... etc.What I want to do is almost the same, except instead of texts, I want to label each point with the image that are in the links of a vector or data frame (in this case in "imgdata"). Note that I selected these images just as examples; I have much more of them, so I can't manually download them. 解决方案 You can use annotation_custom, but it will be a lot of work because each image has to be rendered as a raster object and its location specified. I saved the images as png files to create this example.library(ggplot2)library(png)library(grid)img1 <- readPNG("c:/test/img1.png")g1<- rasterGrob(img1, interpolate=TRUE)img2 <- readPNG("c:/test/img2.png")g2<- rasterGrob(img2, interpolate=TRUE)plotdata<-data.frame(seq(1:2),seq(11:12))ggplot(data=plotdata) + scale_y_continuous(limits=c(0,4))+ scale_x_continuous(limits=c(0,4))+ geom_point(data=plotdata, aes(plotdata[,1],plotdata[,2])) + annotation_custom(g1,xmin=1, xmax=1.5,ymin=1, ymax=1.5)+ annotation_custom(g2,xmin=2, xmax=2.5,ymin=2, ymax=2.5) 这篇关于在ggplot2中用图中的图像标记图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 14:40