**10**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?

**380**• written 6.0 years ago by spaul8505 •

**10**