Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

r - ggplot2-How to add total to a heatmap

I am trying to create a genus level heatmap for microbial community identified in my samples.

Problem

I wanted to add sum and row totals to my heatmap (the total abundance of all genes for each samples and the total abundance of each genes for all samples). However, I struggled to work with the code so that I can add the total values without changing my data and my previous codes.

My attempted workaround

I tried to use the following codes to plot my heatmap. However, the resulting plot was weird.

My data frame:

Gene     Sample1    Sample2   Sample3    Sample4  Sample5   Total
 A        0.0186     1.578     3.478     0.0045    0.569    5.648
 B        0.0009     0.125     1.254     5.890     1.590    8.8599
 C        2.567      0.897     0.0026    1.285     2.648    7.3996
 D        10.421     0.743     0.0152    0.479     6.489    18.1472
...        ...        ...       ...      ...      ...       ...
Total     34.49      11.1      11.72     18.19     24.52    100

My transformed data:

   Gene    Sample   Abundance      
A          1          0.0186        
B          1          0.0009        
C          1          2.567      
D          1          10.421              
...       ...         ...     

     

My code:

p1 <- ggplot(data=heatmap, 
       mapping=aes(x= Sample, y= Gene, fill= Abundance)) + 
  geom_tile() + 
  facet_grid (~ Sample, scales = "free", space = "free") +
  scale_fill_distiller(name = "Relative Abundance (%)", palette = "RdYlGn") + 
  theme(text = element_text(size=12), 
        axis.text.x = element_blank(), 
        axis.text.y = element_text(size = 11, colour = "black"), 
        strip.text.y = element_text(angle = 0), 
        legend.direction = "horizontal", 
        legend.position = "bottom")
+ scale_x_discrete(position = "top") 

p2 <- p1 + geom_point(data = heatmap, aes(color = Abundance),size = 10, shape = 19) 
         + scale_color_gradient2(low = "red", mid = "white",high = "grey", midpoint = 0) 
         + geom_text(data = heatmap, size = 3, aes(label = round(Abundance,2)))
p2

Result:

enter image description here

I appreciate any suggestions on how I could change the plot for better visualizing and any suggestions on how to add the horizontal and vertical sum.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...