0

I need some help to order the variables on heatmap using the ggplot library.

Instead of existing order I want to apply the following order of variables: V10 V9 V8 V7 V6 V1 V2 V3 V4 V5

Many thanks in advance!!!

library(ggplot2)
library(reshape2)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")

ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  labs(x = "", y = "")
camille
  • 16,432
  • 18
  • 38
  • 60
chi2
  • 41
  • 1
  • Make that column a factor and set the order of levels you want. A google search for "site:stackoverflow.com r ggplot manually set order" gets you a lot of options – camille Sep 09 '21 at 16:30

3 Answers3

0

The way in which I would change the order is by making x1 and x2 into factors and assigning their levels.

library(ggplot2)
library(reshape2)
library(data.table)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning- 
databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")
cor.mat.melt$x1 <- factor(cor.mat.melt$x1, levels = c("V10", "V9", "V8", "V7", "V6", 
                                                      "V1", "V2", "V3", "V4", "V5"))
cor.mat.melt$x2 <- factor(cor.mat.melt$x2, levels = c("V10", "V9", "V8", "V7", "V6", 
                                                      "V1", "V2", "V3", "V4", "V5"))



ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
geom_tile(aes(fill = Corelation)) +
scale_fill_gradientn(colours = rainbow(3)) +
geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
labs(x = "", y = "")
Edgar Zamora
  • 456
  • 3
  • 11
0

You can specify the order by setting scale_x/y_discrete(limits = ...).

library(ggplot2)
library(reshape2)
#> Warning: package 'reshape2' was built under R version 4.1.1

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")

cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")

my_order <- paste0("V", c(10, 9, 8, 7, 6, 1, 2, 3, 4, 5))

ggplot(data = cor.mat.melt,
       aes(x = x1, y = x2)) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  scale_x_discrete(limits = my_order) +
  scale_y_discrete(limits = my_order) +
  labs(x = "", y = "")

Created on 2021-09-09 by the reprex package (v2.0.1)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
0

Here's one way you could do it.

library(ggplot2)
library(reshape2)

dt <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep = ",")
cor.mat <- cor(dt[1:10], method = "spearman")
cor.mat.melt <- melt(cor.mat)
colnames(cor.mat.melt) <- c("x1","x2","Corelation")
order <- c('V10', 'V9', 'V8', 'V7', 'V6', 'V1', 'V2', 'V3', 'V4', 'V5')
ggplot(data = cor.mat.melt,
       aes(x =factor(x1, level = order), y =factor(x2, level = order))) +
  geom_tile(aes(fill = Corelation)) +
  scale_fill_gradientn(colours = rainbow(3)) +
  geom_text(aes(x = x1, y = x2, label = round(Corelation, 2))) +
  labs(x = "", y = "")

enter image description here

norie
  • 9,609
  • 2
  • 11
  • 18