我正在尝试这个(https://jcoliver.github.io/learn-r/008-ggplot-dendrograms-and-heatmaps.html)教程,使用ggplot2制作热图和树状图。数据可以在这里找到:https://jcoliver.github.io/learn-r/data/otter-mandible-data.csv.
但是,当我尝试导出高分辨率图像时,
jpeg(file="HeatDendoTest02.jpeg", units="mm", width=775, height=638, res=300)
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))
dev.off()
树状图的排列与热图不合适,我想知道如何解决这个问题。
# Cluster & heatmap on otter data
# Jeff Oliver
# [email protected]
# 2017-08-15
################################################################################
# Load dependencies
library("ggplot2")
library("ggdendro")
library("tidyr")
library("grid")
# Read in data
otter <- read.csv(file = "data/otter-mandible-data.csv",
stringsAsFactors = TRUE)
# Restrict the data to only two species, A. cinerea & L. canadensis
two_species <- c("A. cinerea", "L. canadensis")
otter <- otter[otter$species %in% two_species, ]
# Force re-numbering of the rows after subsetting
rownames(otter) <- NULL
# Scale each measurement (independently) to have a mean of 0 and variance of 1
otter_scaled <- otter
otter_scaled[, c(4:9)] <- scale(otter_scaled[, 4:9])
# Run clustering
otter_matrix <- as.matrix(otter_scaled[, -c(1:3)])
rownames(otter_matrix) <- otter_scaled$accession
otter_dendro <- as.dendrogram(hclust(d = dist(x = otter_matrix)))
# Create dendrogram plot
dendro_plot <- ggdendrogram(data = otter_dendro, rotate = TRUE) +
theme(axis.text.y = element_text(size = 6))
# Heatmap
# Data wrangling
otter_long <- pivot_longer(data = otter_scaled,
cols = -c(species, museum, accession),
names_to = "measurement",
values_to = "value")
# Extract the order of the tips in the dendrogram
otter_order <- order.dendrogram(otter_dendro)
# Order the levels according to their position in the cluster
otter_long$accession <- factor(x = otter_long$accession,
levels = otter_scaled$accession[otter_order],
ordered = TRUE)
# Create heatmap plot
heatmap_plot <- ggplot(data = otter_long, aes(x = measurement, y = accession)) +
geom_tile(aes(fill = value)) +
scale_fill_gradient2() +
theme(axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
legend.position = "top")
# All together
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))
这里有一个选项,使用
patchwork
在导出之前将绘图粘合在一起。尽管如此,将树状图与热图对齐仍然需要一些额外的工作,即我们需要使用 scale_x_continuous
对 y 尺度进行相同的扩展(x
,因为 rotate=TRUE
)。此外,这样做时我们还必须设置 breaks=
和 labels=
.
注意:我使用
ggsave
进行导出并设置 scale=.33
以使文本可读。
library(ggplot2)
library(patchwork)
dendro_plot <- dendro_plot +
scale_x_continuous(
breaks = seq_along(levels(otter_long$accession)),
labels = levels(otter_long$accession),
# Set the same expansion as for the heatmap
expand = c(0, .6)
)
heatmap_plot + dendro_plot +
plot_layout(widths = c(.8, .2))
ggsave(
"HeatDendoTest02.jpeg",
units = "mm", width = 775,
height = 638, dpi = 300,
scale = .33
)