Question: Scatterplots Showing Correlation Between Gene Pairs
1
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:

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 Are there other ways to do this?

R visualization • 3.6k views
modified 6.0 years ago by UnivStudent380 • written 6.0 years ago by spaul850510
1
Irsan7.0k 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

1
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.