当前位置:网站首页>How to use R package ggtreeextra to draw evolution tree

How to use R package ggtreeextra to draw evolution tree

2022-06-24 19:14:00 Yisu cloud

How do you use it? R package ggtreeExtra Draw evolution tree

This article “ How do you use it? R package ggtreeExtra Draw evolution tree ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “ How do you use it? R package ggtreeExtra Draw evolution tree ” Article bar .

introduction

ggplot2 Provided geom_tile Layer can draw heat map ,ggplot2 Of geom_point perhaps ggstar Of geom_star You can draw point layers . In order to extend the ggtree Present relevant data on the outer ring of the phylogenetic tree in the point and layout , Developed ggtreeExtra Package provides a function ,geom_fruit Used to align the graph with the tree , The related charts will be aligned in different positions on the outer panel of the tree . Also developed geom_fruit_list Add multiple layers on the same external panel of the tree . Some functions are based on ggplot2 And support the use of graphical syntax .

Drawing examples

1、 Download and install ggtreeExtra package

if(!requireNamespace("remotes", quietly=TRUE)){  install.packages("remotes")}remotes::install_github("YuLab-SMU/ggtreeExtra")if (!requireNamespace("BiocManager", quietly=TRUE))  install.packages("BiocManager")BiocManager::install("ggtreeExtra")BiocManager::install("ggstar")#  download "ggstar","ggplot2","ggtree","treeio","ggnewscale" package install.packages("ggstar")install.packages("ggplot2")install.packages("ggtree")install.packages("treeio")install.packages("ggnewscale")

2、 Load dependent package

library(ggtreeExtra) #  Set superimposed packages library(ggstar) #  Provide Geometry library(ggplot2) # library(ggtree) #  Draw evolution tree library(treeio)library(ggnewscale) #  Create a new scale, Multiple fill perhaps color

3、 Set up the working directory

setwd("D:/R/ggtreeExtra")

4、 Data sources

#  Tree view data source path trfile <- system.file("extdata", "tree.nwk", package="ggtreeExtra")#  Draw the data source path of point graph and histogram tippoint1 <- system.file("extdata", "tree_tippoint_bar.csv", package="ggtreeExtra")#  The first layer outside the tree draws the heat map to the data source path ring1 <- system.file("extdata", "first_ring_discrete.csv", package="ggtreeExtra")#  The second layer outside the tree draws the heat map to the data source path ring2 <- system.file("extdata", "second_ring_continuous.csv", package="ggtreeExtra")

5、 get data

The tree file is using read . tree Imported . If there are other tree format files , have access to tree io Package to read .

tree <- read.tree(trfile)data = fortify(tree)head(data)

 How do you use it? R package ggtreeExtra Draw evolution tree

6、 Draw a tree view

#  Visual evolutionary tree , The graph here is "fan", It can also be  'rectangular', 'dendrogram', 'slanted', 'ellipse', 'roundrect', 'circular', 'circular', 'inward_circular', 'radial', 'equal_angle', 'daylight' or 'ape'p <- ggtree(tree, layout="fan", open.angle=10, size=0.5)p

 How do you use it? R package ggtreeExtra Draw evolution tree

7、 Get data set plot

dat1 <- read.csv(tippoint1)knitr::kable(head(dat1))dat2 <- read.csv(ring1)knitr::kable(head(dat2))dat3 <-read.csv(ring2)knitr::kable(head(dat3))head(dat3)

dat1 Datasets are used to plot points and bars

 How do you use it? R package ggtreeExtra Draw evolution tree

dat2 Datasets are used to plot heat maps

 How do you use it? R package ggtreeExtra Draw evolution tree

dat3 Datasets are used to plot heat maps

 How do you use it? R package ggtreeExtra Draw evolution tree

a、 Draw a point layer
p2 <- p +   geom_fruit(    data=dat1,    geom=geom_star,    mapping=aes(y=ID, fill=Location, size=Length, starshape=Group),    position="identity",    starstroke=0.2  ) +   scale_size_continuous(    range=c(1, 3), #  Size range     guide=guide_legend(      keywidth=0.5,  #  Box width 0.5      Keyheight=0.5, #  Box width 0.5      override.aes=list(starshape=15),      order=2    )  ) +  scale_fill_manual(    values=c("#F8766D", "#C49A00", "#53B400", "#00C094", "#00B6EB", "#A58AFF", "#FB61D7"),    guide="none"   ) +   scale_starshape_manual(    values=c(1, 15),    guide=guide_legend(      keywidth=0.5,      keyheight=0.5,      order=1    )  )p2

 How do you use it? R package ggtreeExtra Draw evolution tree

b、 Draw a hot layer
p3 <- p2 +   new_scale_fill() +   geom_fruit(    data=dat2,    geom=geom_tile,    mapping=aes(y=ID, x=Pos, fill=Type),offset=0.08,   #  The distance between the outer layers , The default is the... Of the tree  x  Scope 0.03 times .pwidth=0.25  #  The width of the outer layer , The default is the... Of the tree  x  Scope 0.2 times .  ) +   scale_fill_manual(    values=c("#339933", "#dfac03"),guide=guide_legend(keywidth=0.5,keyheight=0.5, order=3)  ) p3

 How do you use it? R package ggtreeExtra Draw evolution tree

c、 Draw a hot layer
p4 <- p3 +   new_scale_fill() +  geom_fruit(    data=dat3,    geom=geom_tile,    mapping=aes(y=ID, x=Type2, alpha=Alpha, fill=Type2),    pwidth=0.15,    axis.params=list(      axis="x", #  Add the axis text of the layer       text.angle=-45, #x  Text angle of the axis hjust=0  #  Adjust the horizontal position of the text axis     )  ) +  scale_fill_manual(    values=c("#b22222", "#005500", "#0000be", "#9f1f9f"),    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=4)  ) +  scale_alpha_continuous(range=c(0, 0.4), # alpha The scope of the guide=guide_legend(keywidth=0.5, keyheight=0.5, order=5)  ) p4

 How do you use it? R package ggtreeExtra Draw evolution tree

d、 Draw a columnar layer
p5 <- p4 +   new_scale_fill() +  geom_fruit(    data=dat1,    geom=geom_bar,mapping=aes(y=ID, x=Abundance, fill=Location),  # dat 1 Of Abundance Will be mapped to  x   pwidth=0.4,    stat="identity",    orientation="y", #  Axis direction     axis.params=list(      axis="x", #  Add the axis text of the layer text.angle=-45, #  The text size of the axis       hjust=0  #  Adjust the horizontal position of the axis text     ),    grid.params=list() #  Add gridlines to the external bar chart   ) +   scale_fill_manual(    values=c("#F8766D", "#C49A00", "#53B400", "#00C094", "#00B6EB", "#A58AFF", "#FB61D7"),    guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)  ) +  theme(#legend.position=c(0.96, 0.5), #  Legend location     legend.background=element_rect(fill=NA), #  Legend background     legend.title=element_text(size=7), #  Legend Title Size     legend.text=element_text(size=6), #  Legend text label size     legend.spacing.y = unit(0.02, "cm")  #  Adjust the y The distance of the axis legend   ) p5

 How do you use it? R package ggtreeExtra Draw evolution tree

That's about “ How do you use it? R package ggtreeExtra Draw evolution tree ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241708084225.html