# ggplot的图形组合与添加jpeg文档的方法

 This post was kindly contributed by 数据科学与R语言 - go there to comment and to read the full post.

`# 读入数据和加载包data <-read.csv('d:\\data.csv',T)library(ggplot2)library(ellipse)library(gridExtra)library(plyr)library(ReadImages) #建立一个函数以生成置信椭圆generatfun <- function(x) {    as.data.frame(with(data,ellipse(cor(a,b),scale=c(sd(a),sd(b)),             level=x,centre=c(mean(a),mean(b)))))}# 根据不同的置信度来生成多个数据框并整合i <-  seq(0.1,0.9,by=0.1)data2 <- adply(i,1,generatfun)names(data2) <- c('level','x','y') # 绘制主图散点图，并将图例去除，这里point层和path层使用了不同的数据集scatter <- ggplot() +     geom_point(data=data,aes(a,b,shape=type))+    geom_path(data=data2,aes(x,y,group=level))+    opts(legend.position = "none")+    geom_vline(xintercept = mean(data\$a),linetype=2)+    geom_hline(yintercept = mean(data\$b),linetype=2)# 绘制上边的直方图，并将各种标注去除hist_top <- ggplot()+geom_histogram(aes(data\$a),colour='black',fill='gray',binwidth = 0.3)+    opts(panel.background=theme_blank(),         axis.title.x=theme_blank(),          axis.title.y=theme_blank(),         axis.text.x=theme_blank(),         axis.text.y=theme_blank(),         axis.ticks=theme_blank())# 同样绘制右边的直方图hist_right <- ggplot()+geom_histogram(aes(data\$b),colour='black',fill='gray',binwidth = 0.1)+    opts(panel.background=theme_blank(),         axis.title.x=theme_blank(),          axis.title.y=theme_blank(),         axis.text.x=theme_blank(),         axis.text.y=theme_blank(),         axis.ticks=theme_blank())+         coord_flip()# 要由四个图形组合而成，可以用空白图作为右上角的图形也可以，但为了好玩加上了R的logo，这是一种在ggplot中增加jpeg位图的方法logo <-  read.jpeg("d:\\Rlogo.jpg")empty <- ggplot(data.frame(x=1:10,y=1:10),aes(x,y))+    annotation_raster(logo,-Inf, Inf, -Inf, Inf)+    opts(axis.title.x=theme_blank(),          axis.title.y=theme_blank(),         axis.text.x=theme_blank(),         axis.text.y=theme_blank(),         axis.ticks=theme_blank())# 最终的组合grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4,1), heights=c(1,4))`

http://stackoverflow.com/questions/2397097/how-can-a-data-ellipse-be-superimposed-on-a-ggplot2-scatterplot
http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2