library(tidyverse)
df_DEG<-read.delim("https://github.com/StatBiomed/BMDS-book/raw/main/notebooks/chapter1-R/Diff_Expression_results.tsv",stringsAsFactors=FALSE)Practice Question 2
Cheatsheets
If you forget ggplot2 syntax, here is a cheatsheet for your reference: ggplot2 Cheatsheet
Question Set 1
*Background: Differentially expressed genes results Diff_Expression_results.tsv (or view here) to this folder.
What is Differential Expression?
Every cell in your body has the same DNA, but different cells use different parts of that DNA to do their jobs. Gene expression is the process where a gene is turned “on” to make proteins that keep the cell running.
Differential expression simply means comparing how active genes are in two different conditions—for example, comparing healthy tissue versus diseased tissue, or treated cells versus untreated cells.
If a gene produces more copies in the diseased state, it is up-regulated. If it produces fewer, it is down-regulated.
- Q1: Use the
summary()function to get an overview of thedf_DEGdata frame.
Answer Key
- Q2: Create a histogram of the
log2FoldChangecolumn indf_DEGusing both the basic plotting functionhist()and theggplot2package. For the ggplot2 plot, add a figure titleHistogram of log2FoldChange.
Answer Key
- Q3: Create a scatter plot with the x-axis as
log10(baseMean)and the y-axis aslog2FoldChangefrom thedf_DEGdata frame. Use both the basicplot()function and theggplot2package.
Answer Key
- Q4: Manipulate the data frame by adding two columns:
- Add a column
log2FC_clipfor clippinglog2FoldChangeto the range[-5, +5]. - Add a column
is_DEto indicate ifpadj < 0.05.
- Add a column
Answer Key
- Q5: Using ggplot2, create a scatter plot colored by the newly added column
is_DE. To make the figure clear, set the color legend title toIs differentially expressedand map the labels fromTRUEtoYesandFALSEtoNo.
Answer Key
- Q6: For the scatter plot from Q5, reverse the color order so that
TRUE (Yes)is blue andFALSE (No)is red. How do you adjust the code? Hint: ggplot uses the order of factor levels to render the color. Usefactor()and set thelevelsparameter to control the order of categories.
Answer Key
- Q7: Save the scatter plot from Q8 or Q9 into a file named “My_DEG_results.pdf”.
Answer Key
Question Set 4
This question set is adapted from the NIH Bioinformatics Training and Education Program. The original question is slightly more complex. If you are interested in more challenging questions, you can visit the website.
Let’s load the Titanic dataset.
| Column | Description |
|---|---|
| Survived | 0 (No) / 1 (Yes) |
| Pclass | 1 / 2 / 3 (Passenger Class) |
| Name | Passenger Name |
| Sex | Male / Female |
| Age | Age |
| Siblings.Spouses.Aboard | Number of siblings/spouses aboard |
| Parents.Children.Aboard | Number of parents/children aboard |
| Fare | Fare Price |
- Q1: Inspect the data using
str(),summary(), andhead().
Answer Key
- Q2: Explore the relationship between the passenger’s age and fare. What type of plot should you use? Write the code using
ggplot2.
Answer Key A scatter plot:
- Q3: Color the points from Q2 by
Pclass. Remember thatPclassis a proxy for socioeconomic status. While the values are treated as numeric upon loading, they are categorical and should be treated as such. You will need to coercePclassinto a categorical (factor) variable usingfactor()oras.factor().
Answer Key
- Q4: Modify the plot from Q3 to update the legend labels to (1 = 1st Class, 2 = 2nd Class, 3 = 3rd Class).
Answer Key