This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to visualize Boxplot with using fold change values in ggplot2 in R

Hi,

I have a previously created Boxplots using normalized log2 data using ggplot2 library in R. Now, I am trying to create the Boxplot using fold change values (example data below) in ggplot2. Could you please assist me how to do the same or provide any useful resources.

dput(head(Data))
structure(list(Genes = c("Gene A", "Gene B", "Gene C", "Gene D", 
"Gene E", "Gene F"), Healthy_Control_p_val = c(1L, 1L, 1L, 1L, 
1L, 1L), Healthy_Control_FC = c(1L, 1L, 1L, 1L, 1L, 1L), Condition_1_p_val = c(0.002651515, 
2.48e-05, 0.73640094, 0.009591015, 0.501723078, 0.226255478), 
    Condition_1_FC = c(-1.114126532, 1.228203535, -1.054172268, 
    -1.255231765, 1.028948329, -1.086890065), Condition_2_p_val = c(0.043930415, 
    0.163740086, 0.798708578, 0.575282124, 0.292885948, 0.008037055
    ), Condition_2_Relapse_FC = c(-1.133444608, 1.163213331, 
    -1.195016491, -1.070544661, 1.051757975, -1.190927361), Condition_3_p_val = c(0.472872641, 
    0.62361238, 0.31757426, 0.481332218, 0.169341523, 0.781283534
    ), Condition_3_FC = c(-1.04283358, 1.04223539, 1.01103926, 
    -1.112300841, 1.074033832, -1.041999594)), row.names = c(NA, 
6L), class = "data.frame")

Thank you,

Toufiq

boxplot ggplot2 r fold change

1 answer

You first need to Convert your data from wide to long format.

library("tidyverse")

df <- df %>%
  select(!ends_with("p_val")) %>%
  pivot_longer(ends_with("FC"), names_to="condition", values_to="fc")

> head(df, 5)
# A tibble: 5 x 3
  Genes  condition                 fc
  <chr>  <chr>                  <dbl>
1 Gene A Healthy_Control_FC      1
2 Gene A Condition_1_FC         -1.11
3 Gene A Condition_2_Relapse_FC -1.13
4 Gene A Condition_3_FC         -1.04
5 Gene B Healthy_Control_FC      1

Now that your data is in long format, you can make any sort of box plot you want. Here's an example.

ggplot(df, aes(x=condition, y=fc)) +
  geom_boxplot() +
  theme(axis.text.x=element_text(angle=45, hjust=1))

Plot of example data.

enter image description here

Hi @rpolicastro, thank you very much for the inputs.

Another question, I wanted to know if there is a way to draw horizontal lines over the Boxplot to indicate or label the p-value significance values as represented in the example plot or this should be manually created.

Log in to answer this question.