散点图中对角轴的直方图

时间:2015-08-01 09:11:17

标签: r ggplot2 histogram scatter-plot

我使用ggplot创建了一个5 x 5散点图矩阵。我为X和Y轴制作了直方图,但我还需要一个额外的矩阵对角线直方图。

编辑数据

data <- structure(c(5, 5, 5, 3, 4, 4, 2, 4, 4, 4, 5, 4, 5, 4, 5, 1, 4, 
3, 5, 4, 5, 2, 3, 3, 3, 4, 2, 5, 2, 4, 3, 3, 3, 3, 5, 4, 3, 4, 
4, 4, 3, 3, 5, 3, 1, 3, 4, 5, 5, 3, 2, 4, 5, 4, 4, 5, 3, 5, 1, 
3, 4, 5, 3, 2, 4, 3, 4, 1, 4, 3, 5, 2, 3, 3, 4, 5, 5, 5, 4, 3, 
1, 1, 4, 2, 5, 4, 4, 1, 5, 3, 4, 2, 4, 3, 4, 4, 5, 4, 5, 1, 4, 
5, 5, 5, 3, 4, 4, 2, 4, 4, 4, 5, 4, 5, 4, 5, 1, 4, 3, 5, 4, 5, 
2, 3, 3, 3, 4, 2, 5, 2, 4, 3, 3, 3, 3, 5, 4, 3, 4, 4, 4, 3, 3, 
5, 3, 1, 3, 4, 5, 5, 3, 2, 4, 5, 4, 4, 5, 3, 5, 1, 3, 3, 5, 2, 
1, 1, 4, 5, 4, 5, 1, 1, 5, 4, 5, 3, 1, 3, 5, 5, 5, 5, 2, 1, 1, 
1, 2, 3, 5, 1, 2, 5, 3, 5, 4, 5, 2, 2, 5, 2, 3, 5), .Dim = c(101L, 
2L))

这是代码

library(ggplot2)
library(gridExtra)

data <- as.data.frame(data) 
x <- data$V2
y <- data$V1              

xhist <- qplot(x, geom="histogram", binwidth = 0.5)  
yhist <- qplot(y, geom="histogram", binwidth = 0.5) + coord_flip() 
none <- 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()) 

g1 <- ggplot(data, aes(x,y)) +
  geom_point(size = 1, position = position_jitter(w=0.3, h=0.3))

grid.arrange(yhist, g1, none, xhist, ncol=2, nrow=2, widths=c(1, 4), heights=c(4,1))

有没有办法直接从这些数据中绘制z轴直方图?我想要的是删除&#39; none&#39;的面板,而是在对角线上放置数据点的直方图。

0 个答案:

没有答案