Currently, I have gene expression data from TCGA and loaded certain genes in a data.frame like this (with T for = tumor sample and N = normal tissue sample):
Gene1 Gene2 Gene3 ...
Patient_T 1 2 3 1
Patient_T 2 1 5 6
Patient_N 1 3 6 1
Patient_N 2 3 6 1
...
I want now to create a grouped boxplot with ggplot2. The graph should depict all the gene candidates in the x-axis and the expression level in the y-axis grouped by tumor and normal for each gene.
In other threads issuing grouped boxplots; they used a different format of the data.frame. I just wondered if there is a practical solution based on this data.frame format to create a grouped plot (ie with the rowname patient_ID).
Note: Biology is not a field I'm comfortable in at all, so please let me know if I've misinterpreted anything in your sample data set.
Reshaping your data from wide to long format - one record per patient, tissue type, and gene - was the key to constructing the grouped boxplots using ggplot2 . In your case, row names of your data frame held two pieces of information: tissue type and the patient ID. Once these were separated into two columns, I gathered all of the Gene1
, Gene2
, and Gene3
columns into two columns: gene
and expression_level
. This is how your original 4 by 3 data frame transformed into a 12 by 4 tidy data set.
# load necessary packages ----
library(tidyverse)
# load necessary data ----
df <-
data.frame(Gene1 = c(2, 1, 3, 3)
, Gene2 = c(3, 5, 6, 6)
, Gene3 = c(1, 6, 1, 1)
, row.names = c("Patient_T 1"
, "Patient_T 2"
, "Patient_N 1"
, "Patient_N 2"))
# reshape data so that it contains one record per: ----
# - patient
# - gene
# - tissue type
tidy.df <-
df %>%
# pid for Patient ID
rownames_to_column(var = "pid") %>%
# only keep the suffix in pid
mutate(pid = str_extract(pid, "(T|N)\\s{1}\\d{1}")) %>%
# separate pid from tissue type in two dif columns
separate(col = "pid"
, into = c("type", "pid")
, sep = "\\s{1}") %>%
gather(key = "gene"
, value = "expression_level"
, matches("Gene")) %>%
# remove 'Gene' from gene column
# and specify the 'type' values
mutate(gene = str_extract(gene, "\\d{1}")
, type = case_when(
type == "N" ~ "Normal"
, type == "T" ~ "Tumor"
)) %>%
# arrange tibble by pid
arrange(pid) %>%
as.tibble()
# create a grouped boxplot with ggplot2 ----
# The graph should depict all the gene candidates
# in the x-axis and the expression level
# in the y-axis grouped by tumor and normal for each gene.
tidy.df %>%
ggplot(aes(x = gene, y = expression_level, fill = gene)) +
geom_boxplot() +
# visualizes the distribution of expression level by gene by tissue type
# i.e. one set of boxplots for nomal and tumor
facet_wrap(facets = vars(type)) +
ylab("Expression level") +
labs(title = "Gene expression data by tissue type"
, caption = "Source: TCGA")
# end of script #
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils
[5] datasets methods base
other attached packages:
[1] bindrcpp_0.2.2 forcats_0.3.0 stringr_1.3.1
[4] dplyr_0.7.6 purrr_0.2.5 readr_1.1.1
[7] tidyr_0.8.1 tibble_1.4.2 ggplot2_3.1.0
[10] tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] tidyselect_0.2.4 haven_1.1.2
[3] lattice_0.20-38 colorspace_1.3-2
[5] htmltools_0.3.6 viridisLite_0.3.0
[7] yaml_2.2.0 utf8_1.1.4
[9] rlang_0.3.0.1 pillar_1.3.0
[11] glue_1.3.0 withr_2.1.2
[13] modelr_0.1.2 readxl_1.1.0
[15] bindr_0.1.1 plyr_1.8.4
[17] munsell_0.5.0 gtable_0.2.0
[19] cellranger_1.1.0 rvest_0.3.2
[21] evaluate_0.11 labeling_0.3
[23] knitr_1.20 fansi_0.3.0
[25] broom_0.5.0 Rcpp_0.12.19
[27] scales_1.0.0 backports_1.1.2
[29] jsonlite_1.5 gridExtra_2.3
[31] hms_0.4.2 digest_0.6.18
[33] stringi_1.2.4 grid_3.5.2
[35] rprojroot_1.3-2 cli_1.0.1
[37] tools_3.5.2 magrittr_1.5
[39] lazyeval_0.2.1 crayon_1.3.4
[41] pkgconfig_2.0.2 xml2_1.2.0
[43] lubridate_1.7.4 assertthat_0.2.0
[45] rmarkdown_1.10 httr_1.3.1
[47] rstudioapi_0.8 viridis_0.5.1
[49] R6_2.2.2 nlme_3.1-137
[51] compiler_3.5.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.