1

I have a 5 x 5 scatterplot matrix that I created using ggplot. I made histograms for X and Y axis, but I needed an additional histogram for the diagonals of the matrix as well.

Edited for data

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

Here is the code

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

Is there a way to directly plot z-axis histogram from this data alone? What I want is to remove the panel of 'none', and instead place a histogram for data points across the diagonal.

Jaap
  • 81,064
  • 34
  • 182
  • 193
new-here
  • 11
  • 3
  • Could you provide a `dput` of your data? See here for more details on how to provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap Aug 01 '15 at 09:35
  • Could you explain better what you mean by *a histogram for data points across the diagonal*? – Jaap Aug 02 '15 at 13:51
  • Something like this http://www.med.upenn.edu/mulab/jpst.html For a matrix of size [n x n], what I wanted was the sum of each element across each of the diagonals. If k = 0, is the central diagonal, then individual sum of elements on kth diagonal, k-1.... and k+1 .... diagonals. What you will get is a [2n-1] vector. I just wondered if there is an inbuilt function in R to do that from. For my own work though, I wrote a for loop. – new-here Aug 02 '15 at 18:18

0 Answers0