导入图像,在顶部放置滤镜,然后绘制数据

时间:2015-10-25 20:01:13

标签: r image ggplot2

我想在R中导入图片的顶部绘制数据。如果可能的话,我还想在图像上放置一个过滤层,比如使用alpha值控制ggplot2中的透明度颜色:

这是一个可以解决的例子:

library(ggplot2)
library(scales)
library(jpeg)
library(scales)
library(grid)

#picture from internet
myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readJPEG(z)
file.remove(z) # cleanup

x <- sample(1:10, replace=T, 10)
y <- c("a","b","c","d","e","f","g","h","i", "j")

df <- data.frame(y,x)

p <-ggplot(df, aes(y, x, fill=y)) + 
  annotation_custom(rasterGrob(pic, width=unit(1,"npc"), height=unit(1,"npc")), 
                    -Inf, Inf, -Inf, Inf) +
  geom_bar(stat = "identity", fill="red",width=0.8, alpha=0.75 )+
  #geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
  ggtitle("Something")+  theme_classic() +
  labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
  theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
  theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
  theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
  theme(panel.grid.major = element_line(colour = "white", linetype = "dotted"))
p

这导致图片:

enter image description here

任何想法?

0 个答案:

没有答案