James Reilly
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # BI278 LAB 8 Notes ## Data Visualization in R (R- bi278.colby.edu) Given example dataset: ![](https://i.imgur.com/PrKnT9E.png) Utilizing a couple different clustering algorithms, we can explore this data. ## Exercise 1. Ordination and Clustering #### 1.1 Principal Components Analysis Ordingation methods are a variety of approaches used to understand multivariate data. - Principal Components Analysis (PCA) aims to take multiple variables of samples and reduce them into smaller number of variables that capture the similarities and differences among the samples. - Non-metric multidimensional scaling (NMDS) looks at distances between pairs of samples and tries to figure out how these samples are arranged in multivariate space. PCA: 1. Utilize the following command to launch the following libraries: ``` x<-c("dendextend", "ggplot2", "vegan", "reshape2", "ggrepel") lapply(x, require, character.only = TRUE) ``` 2. Read in data file as a table named cdata and take a look at it. ``` cdata <- read.table("PATH/multivariate_data.txt", h=T) head(cdata) ``` 3. Set row names of cdata: `rownames(cdata) <- cdata$sample` 4. Make a simple scatterplot. x and y variables can be changed to any of the columns in your object cdata. ``` ggplot(cdata, aes(x=gene1, y=gene2)) + geom_point() + geom_text(aes(label=sample), hjust=-.3, vjust=0) ``` This command will take your table and tell the graphing function ggplot() to use gene1 as x variable and gene 2 as y variable. Then it will make a scatterplot and add some text labels so you can tell which dot is which. 5. To run a PCA on these data and save the results to a new dataframe: ``` cdata.pca <- prcomp(cdata[,c(3:10)], scale=T) cdata.pca.res <- as.data.frame(cdata.pca$x) ``` "3:10" can change in this command. We haven chose to cscale (~normalize) our data. 6. To take a look at the results: `summary(cdata.pca)` Results look like this: ![](https://i.imgur.com/fu3AUSX.png) - What the output indicates is how much of the similarities and differences (variances) of our data are captured in the composite/reduced variables called principal components 1, 2, etc in a cumulative way. Our numbers indicate that PC1 and PC2 are able to capture within those two dimensions 67.6% of the total variance present in our data (last row). 7. To make a scatterplot of these new comoposite variables using the PCA results we captured: ``` ggplot(cdata.pca.res, aes(x=PC1, y=PC2)) + geom_point() + geom_text(aes(label=rownames(cdata.pca.res)), hjust=1,vjust=-.5) ``` 8. To bring the taxon labels over and then add them as a new column to this new dataframe, then make a new scatterplot: ``` cdata.pca.res$taxonomy <- cdata$taxonomy ggplot(cdata.pca.res, aes(x=PC1, y=PC2, color=taxonomy)) + geom_point() + geom_text(aes(label=rownames(cdata.pca.res)), hjust=1,vjust=-.5) ``` 9. If you want to know if the samples are seperated by your experimental variable "path" you can add it as a variable and visualize it: ``` path = c(rep("green",5), rep("blue",4)) cdata.pca.res$path <- path ggplot(cdata.pca.res, aes(x=PC1, y=PC2, color=path)) + geom_point() + geom_text(aes(label=rownames(cdata.pca.res)), hjust=1,vjust=-.5) ``` once again, "taxonomy" as well as "rownames(cdata.pca.res)" are variable in this command. 11. If points or text overlap, this function from ggrepel is a nice way to make things clearer: ``` ggplot(cdata.pca.res, aes(x=PC1, y=PC2, color=path)) + geom_point() + geom_label_repel(aes(label=rownames(cdata.pca.res))) ``` 11. Given how samples fall out on the PCA plot, to find how factors (genes,etc.) were reduced to these variables. We can do this by checking the loadings: `cdata.pca$rotation` - The results of this command show you the relative influences of your factors on each of the PCs (principal components). For Example, I see for PC1 that gene1 and gene7 have higher positive influences while gene3 and gene5 have higher negative influences. So samples that are toward the right-hand side of PC1 should have higher abundances of gene1 and gene7 and lower abundances of gene3 and gene5 compared to other samples. #### 1.2 Nonmetric Multidimensional Scaling NMDS is a popular and robust technique because it is based on ranks rather than normally distributed numbers. 12. First, to turn data into a dissimilarity matrix, we can use Bray-Curtis. Bray Curtis is commonlu used as it handles changes in composition and relative abundances well even whe some data are missing. - We also have to specify number of dimensions (k). It is common practice to choose 2 or 3 dimensions (but you would need to try out a few diff k values and choose the best one that minimizes the stress value) ``` cdata.nms <- metaMDS(cdata[,c(3:10)], distance="bray", k=3) cdata.nms ``` These commands will have R report back how it fit the NMDS including stress value it landed on: ![](https://i.imgur.com/cOHIN6Q.png) Rule of thumb: anything below 0.05 is excellent, < 0.1 is great, < 0.2 is ok, and anything over 0.2 is questionable. 13. We can also examine the Shepard plot which slows the final reduced-dimensio ordination result based on ranks (red line) compared to the original dissimilarities (blue dots). It is good if the blue dots are close to the red line. `stressplot(cdata.nms)` 14. We can plot our results using vegan. But if we want to plot it be differentiating the groups by our experimental variable "path". We can use the built-in method for displaying results like this. ``` path = c(rep("green",5), rep("blue",4)) # same order as your rows plot(cdata.nms, type="n") ordihull(cdata.nms, groups=path, draw="polygon", col="grey90",label=F) orditorp(cdata.nms, display="species", col="red") orditorp(cdata.nms, display="sites", col=c(rep("green",5),rep("blue",4)), cex=1.25) ``` Results of these commands: ![](https://i.imgur.com/kXP1IIw.png) - Unlike PCA, NMDS analyses cannot tell you what the loadings of each axis (NMDS1-X) and sequence of axes is arbitrary (e.g. the largest difference does not necessarily show up on NMDS1 whereas it does on PC1). But we can often draw similar conclusions from NMDS analyses as we would for PCA. Here, it appears the blue group is associated with different abundances of gene1, gene7, and gene8 compared to the green group. ### Steps 16-18 are skipped. If interested look back at the lab (file:///Users/jereil26/Downloads/bi278_fall2022_lab_08.pdf) steps 16-18 for the use of GGPLOT2 for the same purpose as the above steps. example of GGplot2 results: ![](https://i.imgur.com/MXZVbT4.png) #### 1.3 Hierarchical Clustering Hierarchical clustering makes dendrograms very much liek the distance trees we looked at when discussing phylogenies. 18. First, make a (Bray-Curtis) distance matrix: `d <- vegdist(cdata[,3:10], method="bray")` - "3:10" is subject to variability 19. Then, run hierarchical clustering analysis. `cdata.clust <- hclust(d, method="ward.D2")` - You can pick among a few different clustering methods (type in ?hclust for more details; default is “complete”) that differ by how the clusters are evaluated as they are grouped together. Ward’s method tries to find compact spherical clusters, “average” will use the average identities of members of a cluster to represent the cluster, and the “complete” method uses the maximum distance rather than the average distance 20. Take a look at the dendrogram that was produced: `plot(cdata.clust)` The Dendrogram that was produced: ![](https://i.imgur.com/3tMiDU3.png) 21. Next, we cut across this dendrogram to produce the number of clusters that you want. Pick a K based on your guess of how many clusters may be present and cut across the dendrogram. `cutree(cdata.clust, k=2)` - shown here with 2 based on our experimental variable “path”, though remember I made up these data - You can see the classification results based on the K you chose (which class each sample was classified into). - Worth noting: what K you select here did not impact the clustering itself. 22. Now we can use an R package called dendextend to manipulate the hirarchical clustering dendrogram. First, we convert the clustering results into a dendrogram. ``` dendro <- as.dendrogram(cdata.clust) plot(dendro, type = "rectangle", ylab = "Height") ``` Results: ![](https://i.imgur.com/n6a0pke.png) 23. Then, we can manipulate the dendrogram further. For example, we can color the "leaves" based on the cluster identity that was shown with cutree(). ``` dendro %>% set("leaves_pch", c(19)) %>% set("leaves_cex", c(2)) %>% set("leaves_col", as.numeric(cutree(cdata.clust,k=2 [cdata.clust$order])+1) %>% plot(type = "rectangle") ``` - Note: while ggplot2 strings additional functions together with '+', dendextend uses '%>%'.' - This command gave me trouble: this is the command that was successful in the end: ![](https://i.imgur.com/ghnJMsY.png) ### 2. Box Plots and Dot Plots Allows for working with a specific set of genes. Shows how they differ in occurance and abundance among groups. To do this, you have to format your data in "long" format, as opposed to "wide". - instead of each row being a complete set of measures collected for each sample, each row will be a single measure collected for a sample, and any given sample will show up in multiple rows. - This data conversion can be done using reshape2. 24. Convert your data from wide to long to and take a look at how it's been converted so you know the column names. Make sure to add the experimental variable too: ``` cdata <- read.table("PATH/multivariate_data.txt", h=T) path <- c(rep("green",5), rep("blue",4)) cdata$path <- path cluster.long <- melt(cdata[,1:11]) head(cluster.long) ``` "1:11" is subject to variability. 25. Now, we can plot these data into a series of box plots that show us the difference in each. gene count by taxonomy or "path": ``` ggplot(cluster.long, aes(x=taxonomy, y=value, fill=taxonomy)) + geom_boxplot() + facet_wrap(.~variable, scales="free") ggplot(cluster.long, aes(x=path, y=value, fill=path)) + geom_boxplot() + facet_wrap(.~variable, scales="free") ``` Results: ![](https://i.imgur.com/mqS6FYF.png) 26. If you know what gene you want form these, you can subset the data by that gene and make a plot for it: ``` gene2.long <- subset(cluster.long, variable=="gene2") ggplot(gene2.long, aes(x=taxonomy, y=value, fill=taxonomy)) + geom_boxplot() ``` Results: ![](https://i.imgur.com/nOpnLzy.png) 27. You may want to make a dot plot instead of a box plot to better show the individual values: ``` ggplot(gene2.long, aes(x=path, y=value, fill=path)) + geom_dotplot(binaxis='y', stackdir='center') ``` Results: ![](https://i.imgur.com/E0duw3v.png) ### 3. Applying Dendextend to Other Trees To use dendextend on trees you may have built with FastTree, IQ-Tree or any other phylogenetic package, you will need to convert the newick tree file format to a dendrogram format. The R package DECIPHER is able to do this. 1. First read a tree you made and plot it. ``` library(DECIPHER) dendro.ex <- ReadDendrogram("PATH/k25.tre") plot(dendro.ex) ``` "k25.tre" is the tree file. My results using lab4 tree: ![](https://i.imgur.com/85hKaja.png) 2. To increase the margins in the plot window, then plotting again to test if able to see labels: ``` par(mar = c(12,4,2,2) + 0.1) #bottom, left, top, right; default is c(5,4,4,2) plot(dendro.ex) ``` You can then color the taxa ("leaves"), to show which cluster they are in, or whatever else. 3. To first find out how many taxa are on this tree I can call the tree object itself: `dendro.ex` ![](https://i.imgur.com/fwDwFlf.png) I find mine has 63 members total. 4. Now you can change the shape (fig4) and colors (fig5) of the leaves manually. For a tree with 15 members total: ``` dendro.ex %>% set("leaves_pch", c(15,16,16,16,16,16,16,16,16,17,17,17,17,15,15)) %>% set("leaves_col", c(4,3,3,3,3,3,3,3,3,2,2,2,2,4,4)) %>% plot(type="rectangle") ``` ![](https://i.imgur.com/mfy1vwV.png) 5. You can also turn the plots sideways: (for the one w 15) ``` par(mar = c(5,4,4,12)) dendro.ex %>% set("leaves_pch", c(15,16,16,16,16,16,16,16,16,17,17,17,17,15,15)) %>% set("leaves_col", c(4,3,3,3,3,3,3,3,3,2,2,2,2,4,4)) %>% plot(type="rectangle", horiz=TRUE) ``` 6. You can also change the colors of the labels instead: ``` dendro.ex %>% set("leaves_pch", c(16)) %>% set("leaves_col", c(4,3,3,3,3,3,3,3,3,2,2,2,2,4,4)) %>% set("labels_colors", c(4,3,3,3,3,3,3,3,3,2,2,2,2,4,4)) %>% plot(type="rectangle", horiz=TRUE) ``` 7. It might be helpful to sort your taxa by increasing node depth: ``` dendro.ex %>% set("labels_colors", c(4,3,3,3,3,3,3,3,3,2,2,2,2,4,4)) %>% sort(type = "nodes") %>% plot(type="rectangle", horiz=TRUE) ``` 8. This command sets the margins back to the default after wards. `par(mar = c(5,4,4,2))`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully