将自定义图像显示为geom_point [英] Display custom image as geom_point

查看:222
本文介绍了将自定义图像显示为geom_point的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在R ggplot中将自定义图像(比如png格式)显示为geom_point?

  library(png)
pic1< - readPNG(pic1.png)

png(Heatmap.png,units =px,width = 3200,height = 3200,res = 300)
ggplot(data_frame,aes(medium,day,fill = Transactions)+
geom_tile(color =white)+
facet_grid(dime3_year〜dime3_month)+
scale_fill_gradient(high =blue,low =white)+
theme_bw()+
geom_point(aes(dime3_channel,day,size = Conv,alpha = Conv,image =(annotation_raster(pic1,xmin = 0,ymin = 0,xmax = 5,ymax = 5)),color =firebrick) )+

出错:


不知道如何为
proto / environment类型的对象自动挑选比例。默认为连续错误:美学必须
的长度为1或与
dataProblems的长度相同:(annotation_raster(conv_pic,xmin = 0,ymin = 0,xmax =
5,ymax = 5))


解决方案

点geom用于创建散点图,似乎是设计来做你需要的,即显示自定义图像。但是,此处回答了类似的问题,这表明问题可以通过以下步骤解决:

(1)阅读您要显示的自定义图像,

(2)使用 rasterGrob()函数渲染给定位置,大小和方向的栅格对象,



(3)使用绘图功能,例如 qplot()



(4)使用诸如 annotation_custom()用作静态注释,指定user20650提到的x和y限制的粗略调整。



使用下面的代码,我可以得到两个定制图像img1.png和img2.png,它们位于给定的xmin,xmax,ymin和ymax处。

  library(png)
library(ggplot2)
library(gridGraphics)
setwd(c:/ MyFolder /)

img1< - readPNG(img1.png)
img2< - readPNG(img2.png)
g1< - rasterGrob img1,interpolate = FALSE)
g2 < - rasterGrob(img2,interpolate = FALSE)
qplot(1:10,1:10,geom =blank)+
annotation_custom(g1 ,xmin = 1,xmax = 3,ymin = 1,ymax = 3)+
annotation_custom(g2,xmin = 7,xmax = 9,ymin = 7,ymax = 9)+
geom_point


Is it possible to display custom image (say png format) as geom_point in R ggplot?

library(png)
pic1 <- readPNG("pic1.png")

png("Heatmap.png", units="px", width=3200, height=3200, res=300)
ggplot(data_frame, aes(medium, day, fill = Transactions))  +
   geom_tile(colour="white")  +
   facet_grid(dime3_year~dime3_month) + 
   scale_fill_gradient(high="blue",low="white") +
   theme_bw() + 
   geom_point(aes(dime3_channel, day, size=Conv,alpha=Conv,image=(annotation_raster(pic1,xmin=0,ymin=0,xmax=5,ymax=5)),color="firebrick")) +

Gives error:

Don't know how to automatically pick scale for object of type proto/environment. Defaulting to continuous Error: Aesthetics must either be length one, or the same length as the dataProblems:(annotation_raster(conv_pic, xmin = 0, ymin = 0, xmax = 5, ymax = 5))

解决方案

The point geom is used to create scatterplots, and doesn't quite seem to be designed to do what you need, ie, display custom images. However, a similar question was answered here, which indicates that the problem can be solved in the following steps:

(1) Read the custom images you want to display,

(2) Render raster objects at the given location, size, and orientation using the rasterGrob() function,

(3) Use a plotting function such as qplot(),

(4) Use a geom such as annotation_custom() for use as static annotations specifying the crude adjustments for x and y limits as mentioned by user20650.

Using the code below, I could get two custom images img1.png and img2.png positioned at the given xmin, xmax, ymin, and ymax.

library(png)
library(ggplot2)
library(gridGraphics)
setwd("c:/MyFolder/")

img1 <- readPNG("img1.png")
img2 <- readPNG("img2.png")
g1 <- rasterGrob(img1, interpolate=FALSE)
g2 <- rasterGrob(img2, interpolate=FALSE)
qplot(1:10, 1:10, geom="blank") + 
  annotation_custom(g1, xmin=1, xmax=3, ymin=1, ymax=3) +
  annotation_custom(g2, xmin=7, xmax=9, ymin=7, ymax=9) +  
  geom_point()

这篇关于将自定义图像显示为geom_point的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆