这个问题在这里已经有了答案:
How to use an image as a point in ggplot?
(3 个回答)
3年前关闭。
是否可以在 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(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")) +
给出错误:
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))
最佳答案
点 geom 用于创建散点图,似乎并没有完全按照您的需要设计,即显示自定义图像。然而,类似的问题得到了回答 here ,这表明可以通过以下步骤解决问题:
(1) 读取要显示的自定义图像,
(2) 使用 rasterGrob()
在给定的位置、大小和方向渲染光栅对象功能,
(3) 使用绘图函数如qplot()
,
(4) 使用geom,例如annotation_custom()
用作静态注释,指定 user20650 提到的 x 和 y 限制的粗略调整。
使用下面的代码,我可以在给定的 xmin、xmax、ymin 和 ymax 处获得两个自定义图像 img1.png 和 img2.png。
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()
关于r - 将自定义图像显示为 geom_point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637455/