Kuo-Hsien Chang
Kuo-Hsien Chang

Reputation: 935

creating continuous heatmap on a square figure for a dataset with uneven length of x and y

Any ways to fit a dataset with uneven length of x and y on a square figure for creating a continuous heatmap?

The dataset has a dataset of 10 rows (y axis) by 1000 columns (x axis). My intention is to plot a continuous heatmap and this heatmap should be a square using ggplot2.

Here is the example:

df<- data.frame(matrix(runif(10000,1,100), nrow=10))

Thanks a lot.

Upvotes: 0

Views: 80

Answers (1)

JasonWang
JasonWang

Reputation: 2434

You need to transform the data into long format. You can use melt function from reshape2 package to do it.

library(ggplot2)
library(reshape2)

m <- matrix(runif(10000, 1, 100), nrow=10)
ggplot(melt(m), aes(x=Var2, y=Var1, fill=value)) + geom_tile()

enter image description here

You can change the plot into square by ratio argument in coord_fixed.

# Adjust the ratio
ggplot(melt(m), aes(x=Var2, y=Var1, fill=value)) + geom_tile() +
  coord_fixed(ratio=100)

enter image description here

Upvotes: 2

Related Questions