base-plot
参考地址:
参考1; 参考2
- png
library(png)
download.file("http://rprogramming.net/wp-content/uploads/2012/10/R-Programming.png", "R.png")
image <- readPNG("R.png")
plot(1:2, type="n", main="Plotting Over an Image", xlab="x", ylab="y", asp=1)
lim <- par()
rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1.5, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")
- jpeg
library(jpeg)
download.file("https://upload.wikimedia.org/wikipedia/commons/a/ab/Takia_Sheikhan's_Beauty.jpg", "R.jpg")
image <- readJPEG("R.jpg")
plot(1:2, type="n", main="Plotting Over an JPEG", xlab="x", ylab="y", asp=1)
lim <- par()
rasterImage(image, 1, 1, 2, 2)
points(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1.5, 1.3, 1.7, 1.6, 1.7, 1.0), col="red")
- gif
library(caTools)
Gif <- read.gif("http://www.openbsd.org/art/puffy/ppuf600X544.gif")
n <- dim(Gif$image)
image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
op <- par(new=T)
plot(1:100,new=T)
par(op)
ggplot2
参考1; 参考2
library(png)
download.file("http://fu2.sdo.com/18/1218/1405/25/67/9274_15564467.PNG", "R.png")
image <- readPNG("R.png")
pp <- ggplot(dat_4f_t, aes(pos_x, pos_y)) +
annotation_custom(rasterGrob(image, width=unit(1,"npc"), height=unit(1,"npc"))) +
xlim(0, 125) + ylim(0, 55) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
xlab("") + ylab("") +
theme(axis.ticks = element_blank(), axis.text = element_blank())