0

I have clustered my dataframe with a heirarchical clustering, however my the cluster diagram and the axis from the heatmap differ strongly. I'm trying to make these the same. However this is not working properly.

The DataFrame (dput() output)

structure(list(ï..X1 = structure(c(10L, 11L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L), .Label = c("GCF1398", "GCF1469", "GCF1471", "GCF1542", 
"GCF1550", "GCF1555", "GCF1557", "GCF1565", "GCF1567", "GCF2029", 
"GCF2685", "GCF2760", "GCF2767", "GCF2775", "GCF2791", "GCF2808", 
"GCF2815", "GCF3517", "GCF3579", "GCF3734", "GCF3737"), class = "factor"), 
    X2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "GCF2983", class = "factor"), 
    value = c(1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
    1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-21L))

Dataframe

enter image description here

df <- df.lower
df$X1 <- gsub("GCF", "", df$X1)
df$X2 <- gsub("GCF", "", df$X2)
df_wide <- reshape2::dcast(df.lower, formula = X1~X2, value.var = 'value')
rownames(df_wide) = df_wide$X2
m <- as.matrix(df_wide[,-1])
df.dendro <- as.dendrogram(hclust(d = dist(x = m)))

# Create dendroplot
dendro.plot <- ggdendrogram(data = df.dendro, rotate = TRUE)
#Resize Axis Elements
dendro.plot <- dendro.plot + theme(axis.text.y = element_text(size = 6))
# Preview the plot
print(dendro.plot)

# Heatmap and Data wrangling
heatmap.plot <- ggplot(df.lower, aes(X1, X2)) + geom_tile(aes(fill = value)) + theme_ipsum() +
  theme(axis.text.y = element_text(size = 6), axis.text.x = element_text(size = 6, hjust = 1, angle = 90),
        legend.position = "top")
heatmap.plot

grid.newpage()
print(heatmap.plot, vp = viewport(x = 0.4, y = 0.5, width = 0.8, height = 1.0))
print(dendro.plot, vp = viewport(x = 0.90, y = 0.43, width = 0.2, height = 0.92))
Community
  • 1
  • 1

1 Answers1

0

You need to order your X1 according to the order in the dendrogram before you plot, otherwise it's ordered alphabetically (or whatever is in your dataframe). Not very sure how you want to orientate your dendrogram and heatmap, but basically something like below, you might need to reverse the order:

m <- as.matrix(df_wide[,-1])
rownames(m) = df_wide[,1]
df.dendro <- as.dendrogram(hclust(d = dist(x = m)))
dendro.plot <- ggdendrogram(data = df.dendro, rotate = TRUE)
dendro.plot <- dendro.plot + theme(axis.text.y = element_text(size = 6))

# Heatmap and Data wrangling
heatmap.plot <- ggplot(df.lower, aes(X2,factor(X1,levels=labels(df.dendro)))) + 
geom_tile(aes(fill = value)) + theme_ipsum() +
  theme(axis.text.y = element_text(size = 6), axis.text.x = element_text(size = 6, hjust = 1, angle = 90),
        legend.position = "top")

grid.newpage()
print(heatmap.plot, vp = viewport(x = 0.4, y = 0.5, width = 0.8, height = 1.0))
print(dendro.plot, vp = viewport(x = 0.90, y = 0.43, width = 0.2, height = 0.92))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72