Scatterplots Showing Correlation Between Gene Pairs
2
2
Entering edit mode
10.6 years ago
spaul8505 ▴ 20

I have two dataframes

 x<-data.frame(matrix(rnorm(1000), nrow=1000, ncol=10))
 y<-data.frame(matrix(rnorm(1000), nrow=1000, ncol=10))

where the rows are the genes and the columns are samples. I wanted to produce a scatterplot showing the correlation between all gene-pairs in " x" (x-axis) relative to the correlation of the same gene in "y" (y-axis).

I transposed the dataframes to calculate gene corrleations and adopted the following code to produce the scatterplot:

 x<-t(x)
 y<-t(y)  

  DF <- data.frame(x,y)

 # Calculate 2d density over a grid
 library(MASS)
 dens <- kde2d(x,y)

 # create a new data frame of that 2d density grid
 gr <- data.frame(with(dens, expand.grid(x,y)), as.vector(dens$z))
 names(gr) <- c("xgr", "ygr", "zgr")

# Fit a model
 mod <- loess(zgr~xgr*ygr, data=gr)

# Apply the model to the original data to estimate density at that point DF$pointdens <- predict(mod, newdata=data.frame(xgr=x, ygr=y))

# Draw plot
library(ggplot2)
ggplot(DF, aes(x=x,y=y, color=pointdens)) + geom_point() +scale_colour_gradientn(colours =  rainbow(5)) + theme_bw()

But when I apply the following code to my transposed data, I get the error

(list) object cannot be coerced to type 'double'

The plot am trying to produce look similar to this

Density plot of gene correlation

Are there other ways to do this?

r visualization • 5.0k views
ADD COMMENT
1
Entering edit mode
10.6 years ago
Irsan ★ 7.8k

What you want to do is to plot an all-vs-all correlation matrix plot. There is a package called GGally that takes as input your matrix with genes as rows and columns as samples, calculates all the possible sample-vs-samples correlations and plots it with ggplot2. When you install it, make sure your R version is up to date, GGally depends on R version > 3.0

ADD COMMENT
1
Entering edit mode
10.6 years ago
UnivStudent ▴ 430

You might also want to try using the stat_density2d function in ggplot2. It seems like it might be a simpler way of doing this. Probably would save you the trouble of doing this yourself with the 2d density. There are some good examples in the documentation which I've linked.

ADD COMMENT

Login before adding your answer.

Traffic: 2632 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6