admin管理员组文章数量:1122846
I am trying to make a heatmap in R for species in a family of microorganisms with samplesID as x, species names as y, and two faceting factor groups at the top. I want something like the picture below
Here is what I have been able to do so far:
ad = tidyr::pivot_wider(Intrasporangiaceae, names_from = 'Species',values_from='Abundance')
ad[is.na(ad)] <- 0
ad = as.matrix(ad[,-c(1:11)])
adscaled <- scale(ad)
Aclust <- hclust(dist(t(ad)))
ggplot(Intrasporangiaceae, aes(Sorghum_Variety, Species, fill = Abundance)) +
geom_tile(width=1.0) +
facet_grid(~Striga_Infestation_Status) +
scale_fill_gradientn(name= 'Abundance',
colours = c("#373898ff", "pink","#c11630ff"),
values = c(0,0.1,1), expand = c(0,0), limit = c(0,NA)) +
scale_y_discrete(limits = colnames(ad)[Aclust$order]) +
labs(title = paste('Species Abundance in', family_Name), x='', y='') +
theme(legend.position='right',panel.background=element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = 8,angle = 90, hjust = 1),
axis.text.y= element_markdown(),
legend.text = element_text(size=7),
legend.key.height = unit(10,'pt'),
legend.title = element_text(size = 10),
strip.text = element_text(size = 10),
axis.title.x = element_text(size = 8),
axis.title.y = element_text(size = 10),
plot.title = element_text(size=15, hjust = 0.5)) +
coord_fixed(ratio = 0.5) +
theme(panel.spacing = unit(0.04, 'lines'))
What I want: I look forward to your help. Thanks in advance. A sample data is available here: `
I am trying to make a heatmap in R for species in a family of microorganisms with samplesID as x, species names as y, and two faceting factor groups at the top. I want something like the picture below
Here is what I have been able to do so far:
ad = tidyr::pivot_wider(Intrasporangiaceae, names_from = 'Species',values_from='Abundance')
ad[is.na(ad)] <- 0
ad = as.matrix(ad[,-c(1:11)])
adscaled <- scale(ad)
Aclust <- hclust(dist(t(ad)))
ggplot(Intrasporangiaceae, aes(Sorghum_Variety, Species, fill = Abundance)) +
geom_tile(width=1.0) +
facet_grid(~Striga_Infestation_Status) +
scale_fill_gradientn(name= 'Abundance',
colours = c("#373898ff", "pink","#c11630ff"),
values = c(0,0.1,1), expand = c(0,0), limit = c(0,NA)) +
scale_y_discrete(limits = colnames(ad)[Aclust$order]) +
labs(title = paste('Species Abundance in', family_Name), x='', y='') +
theme(legend.position='right',panel.background=element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = 8,angle = 90, hjust = 1),
axis.text.y= element_markdown(),
legend.text = element_text(size=7),
legend.key.height = unit(10,'pt'),
legend.title = element_text(size = 10),
strip.text = element_text(size = 10),
axis.title.x = element_text(size = 8),
axis.title.y = element_text(size = 10),
plot.title = element_text(size=15, hjust = 0.5)) +
coord_fixed(ratio = 0.5) +
theme(panel.spacing = unit(0.04, 'lines'))
What I want: I look forward to your help. Thanks in advance. A sample data is available here: https://drive.google.com/file/d/1Y-D2h3mYNOpBjAPaqdLmTcGtHtOS-F2z/view?usp=sharing`
Share Improve this question edited Nov 21, 2024 at 16:17 Seth 3,8292 gold badges6 silver badges15 bronze badges asked Nov 21, 2024 at 13:53 user8019548user80195482 Answers
Reset to default 1I recently developed a ggplot2 extension ggalign by providing advanced tools for aligning and organizing multiple plots, particularly those that automatically reorder observations, such as dendrogram. It offers fine control over layout adjustment and plot annotations, enabling you to create complex, publication-quality visualizations while still using the familiar grammar of ggplot2.
if you are interested, please check it out at https://github.com/Yunuuuu/ggalign!
For documents of the release version, please see https://yunuuuu.github.io/ggalign/, for documents of the development version, please see https://yunuuuu.github.io/ggalign/dev/.
The development version now seamlessly integrates with maftools and supports magick rasterization.
Though it now cannot support nested facet, it's easy to add the feature. I'm not sure if nested facet is an urgent need since we can use tile to indicates groups easily.
data <- readr::read_csv("intrapsoragiaceae.csv")
ad <- tidyr::pivot_wider(data,
id_cols = "Sample",
names_from = "Species", values_from = "Abundance"
)
names(ad)
ad <- t(as.matrix(tibble::column_to_rownames(ad, "Sample")))
facetted_data <- dplyr::select(
data, Sample,
Sorghum_Variety, Striga_Infestation_Status
)
facetted_data <- unique(facetted_data)
facetted_data <- facetted_data[match(colnames(ad), facetted_data$Sample), ]
colnames(ad) == facetted_data$Sample
ggheatmap(ad, height = 1) +
theme(axis.text.x = element_text(angle = -60, hjust = 0)) +
anno_top(size = 0.1) +
align_group(facetted_data$Sorghum_Variety) +
ggalign(data = NULL) +
geom_tile(aes(.x, 1, fill = .panel)) +
scale_fill_brewer(name = "Sorghum Variety", palette = "Dark2") +
ggalign(data = NULL) +
plot_data(function(x) {
x$group <- facetted_data$Striga_Infestation_Status[x$.index]
x
}) +
geom_tile(aes(.x, 1, fill = group)) +
scale_fill_brewer(name = "Striga Infestation", palette = "Set1") +
anno_right(size = 0.2) +
align_dendro() &
theme(plot.margin = margin())
The ggh4x
package can help you create a heat map with nested facets:
Since coord_fixed
is incompatible with free scales in facet_
functions, you can specify the desired aspect ratio as one of your theme's components.
library(ggplot2)
aspect_ratio <- length(unique(df$Species))
ggplot(df, aes(SampleID, Species, fill = Abundance)) +
geom_tile(width = 1, height = 1) +
scale_fill_gradientn(name= 'Abundance',
colours = c("#373898ff", "pink","#c11630ff"),
values = c(0,0.1,1), expand = c(0,0), limit = c(0,NA)) +
ggh4x::facet_nested(~ Sorghum_Variety + Striga_Infestation_Status,
space = 'free_x',
scales = 'free_x'
) +
labs(y = '') +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
axis.text.y = element_text(angle = -45, vjust = 1),
aspect.ratio = aspect_ratio,
legend.position = 'bottom')
本文标签: ggplot2Heatmap with Annotation Using GGplotStack Overflow
版权声明:本文标题:ggplot2 - Heatmap with Annotation Using GGplot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310274a1934319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论