Practice Question 2

Author

Dr. Yu Cheng Hsu

Published

October 8, 2026

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.

library(tidyverse)
df_DEG<-read.delim("https://github.com/StatBiomed/BMDS-book/raw/main/notebooks/chapter1-R/Diff_Expression_results.tsv",stringsAsFactors=FALSE)
  • Q1: Use the summary() function to get an overview of the df_DEG data frame.

Answer Key

  • Q2: Create a histogram of the log2FoldChange column in df_DEG using both the basic plotting function hist() and the ggplot2 package. For the ggplot2 plot, add a figure title Histogram of log2FoldChange.

Answer Key

  • Q3: Create a scatter plot with the x-axis as log10(baseMean) and the y-axis as log2FoldChange from the df_DEG data frame. Use both the basic plot() function and the ggplot2 package.

Answer Key

  • Q4: Manipulate the data frame by adding two columns:
    • Add a column log2FC_clip for clipping log2FoldChange to the range [-5, +5].
    • Add a column is_DE to indicate if padj < 0.05.

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 to Is differentially expressed and map the labels from TRUE to Yes and FALSE to No.

Answer Key

  • Q6: For the scatter plot from Q5, reverse the color order so that TRUE (Yes) is blue and FALSE (No) is red. How do you adjust the code? Hint: ggplot uses the order of factor levels to render the color. Use factor() and set the levels parameter 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(), and head().

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 that Pclass is 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 coerce Pclass into a categorical (factor) variable using factor() or as.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