r - ggplot2 中带有边缘直方图的散点图

标签 r ggplot2 histogram scatter-plot

是否有一种方法可以像下面的 ggplot2 示例中那样创建带有边缘直方图的散点图?在 Matlab 中,它是 scatterhist() 函数,并且 R 也存在等效函数。但是,我还没有在 ggplot2 中看到它。

scatterplot with marginal histograms

我开始尝试创建单个图表,但不知道如何正确排列它们。

 require(ggplot2)
 x<-rnorm(300)
 y<-rt(300,df=2)
 xy<-data.frame(x,y)
     xhist <- qplot(x, geom="histogram") + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 5/16, axis.text.y = theme_blank(), axis.title.y=theme_blank(), background.colour="white")
     yhist <- qplot(y, geom="histogram") + coord_flip() + opts(background.fill = "white", background.color ="black")

     yhist <- yhist + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 16/5, axis.text.y = theme_blank(), axis.title.y=theme_blank() )


     scatter <- qplot(x,y, data=xy)  + scale_x_continuous(limits=c(min(x),max(x))) + scale_y_continuous(limits=c(min(y),max(y)))
none <- qplot(x,y, data=xy) + geom_blank()

并使用发布的函数 here 来排列它们。但长话短说:有没有办法创建这些图表?

最佳答案

这不是一个完全响应式的答案,但它非常简单。它说明了显示边缘密度的替代方法以及如何使用 Alpha 级别进行支持透明度的图形输出:

scatter <- qplot(x,y, data=xy)  + 
         scale_x_continuous(limits=c(min(x),max(x))) + 
         scale_y_continuous(limits=c(min(y),max(y))) + 
         geom_rug(col=rgb(.5,0,0,alpha=.2))
scatter

enter image description here

关于r - ggplot2 中带有边缘直方图的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8545035/

相关文章:

c - 用于一维 double 据数组的简单 ANSI C 直方图函数?

r - 使用 lm() 和 scale() 的标准化回归系数不同于使用 lm.beta() 或 cor() 的回归系数

在没有 Shiny 服务器的 Docker 中运行 Shinyapp

r - 使用ggrepel覆盖水平定位

r - 非分面图中的分面式标题

r - 拆分数据以在 R 中并排绘制直方图

R聚类-带有观察标签的轮廓

R:在函数中需要有限的 'ylim'值

r - 小的 ggplot 对象 (1 mb) 保存后变成 7 GB 的 .Rdata 对象

c++ - 用于 C++ MatND 的 OpenCV cvGetMinMaxHistValue