Question: Scatterplots Showing Correlation Between Gene Pairs
gravatar for spaul8505
6.7 years ago by
United States
spaul850510 wrote:

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:


  DF <- data.frame(x,y)

 # Calculate 2d density over a grid
 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
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 • 3.9k views
ADD COMMENTlink modified 6.7 years ago by UnivStudent380 • written 6.7 years ago by spaul850510
gravatar for Irsan
6.7 years ago by
Irsan7.1k wrote:

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 COMMENTlink modified 6.7 years ago • written 6.7 years ago by Irsan7.1k
gravatar for UnivStudent
6.7 years ago by
UnivStudent380 wrote:

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 COMMENTlink written 6.7 years ago by UnivStudent380
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 904 users visited in the last hour