ggplot2中具有边缘直方图的散点图 [英] Scatterplot with marginal histograms in ggplot2

查看:853
本文介绍了ggplot2中具有边缘直方图的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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

  require(ggplot2)
x <-rnorm(300)
y <-rt(300,df = 2)
xy < ; -data.frame(x,y)
xhist <-qplot(x,geom =直方图)+ 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 =直方图)+ 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,a xis.text.y = theme_blank(),axis.title.y = theme_blank())


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

并将它们与发布的函数进行排列此处。但是要长话短说:有没有创建这些图的方法?

gridExtra 包应该在这里工作。开始制作每个ggplot对象:

$ $ p $ hist_top < - ggplot()+ geom_histogram(aes(rnorm(100)) )
空< - ggplot()+ geom_point(aes(1,1),color =white)+
theme(axis.ticks = element_blank(),
panel.background = element_blank(),
axis.text.x = element_blank(),axis.text.y = element_blank(),
axis.title.x = element_blank(),axis.title.y = element_blank ())

scatter < - ggplot()+ geom_point(aes(rnorm(100),rnorm(100)))
hist_right< - ggplot()+ geom_histogram(aes rnorm(100)))+ coord_flip()

然后使用grid.arrange函数:

  grid.arrange(hist_top,empty,scatter,hist_right,ncol = 2,nrow = 2,widths = c(4,1), heights = c(1,4))


Is there a way of creating scatterplots with marginal histograms just like in the sample below in ggplot2? In Matlab it is the scatterhist() function and there exist equivalents for R as well. However, I haven't seen it for ggplot2.

I started an attempt by creating the single graphs but don't know how to arrange them properly.

 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()

and arranging them with the function posted here. But to make long story short: Is there a way of creating these graphs?

解决方案

The gridExtra package should work here. Start by making each of the ggplot objects:

hist_top <- ggplot()+geom_histogram(aes(rnorm(100)))
empty <- ggplot()+geom_point(aes(1,1), colour="white")+
         theme(axis.ticks=element_blank(), 
               panel.background=element_blank(), 
               axis.text.x=element_blank(), axis.text.y=element_blank(),           
               axis.title.x=element_blank(), axis.title.y=element_blank())

scatter <- ggplot()+geom_point(aes(rnorm(100), rnorm(100)))
hist_right <- ggplot()+geom_histogram(aes(rnorm(100)))+coord_flip()

Then use the grid.arrange function:

grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))

这篇关于ggplot2中具有边缘直方图的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆