The solutions given below are just one way of obtaining the desired plots! There are probably several different ways you could code to get the same plots.
For the exercises in the first two lab sessions: R introduction
and the ggplot basics 1
, all you need to do is to replace the filename raw_counts.txt
with each of the different files to look at the differences between different normalization methods.
Task Plot 1:
gc_raw <- read.table(file = "data/counts_raw.txt", sep = "\t", header = T)
gc_filt <- read.table(file = "data/counts_filtered.txt", sep = "\t", header = T)
gc_vst <- read.table(file = "data/counts_vst.txt", sep = "\t", header = T)
gc_deseq <- read.table(file = "data/counts_deseq2.txt", sep = "\t", header = T)
md <- read.table("data/metadata.csv", header = T, sep = ";")
gene_counts_all <-
gc_raw %>% gather(Sample_ID, Raw, -Gene) %>%
full_join(gc_filt %>% gather(Sample_ID, Filtered, -Gene), by = c("Gene", "Sample_ID")) %>%
full_join(gc_vst %>% gather(Sample_ID, VST, -Gene), by = c("Gene", "Sample_ID")) %>%
full_join(gc_deseq %>% gather(Sample_ID, DESeq2, -Gene), by = c("Gene", "Sample_ID")) %>%
gather(Method, count, Raw:DESeq2) %>%
filter(!is.na(count)) %>%
full_join(md, by = "Sample_ID")
gene_counts_all$Time <- factor(gene_counts_all$Time, levels = c("t0","t2","t6","t24"))
gene_counts_all$Replicate <- factor(gene_counts_all$Replicate, levels = c("A","B","C"))
gene_counts_all$Method <- factor(gene_counts_all$Method, levels = c("Raw","Filtered","DESeq2","VST"))
gene_counts_all %>%
group_by(Time, Replicate, Method) %>%
summarise(mean=mean(log10(count +1)),se=se(log10(count +1))) %>%
ggplot(aes(x= Time, y= mean, fill = Replicate)) +
geom_bar(position = position_dodge2(), stat = "identity") +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se), position = position_dodge2(.9, padding = .6)) +
facet_wrap(~Method, scales = "free")
Task Plot 2:
gene_counts_all %>%
group_by(Time, Replicate, Method) %>%
ggplot() +
geom_boxplot(mapping = aes(x = Sample_Name, y = log10(count + 1), fill = Time)) +
facet_wrap(~Method*Replicate, ncol = 3, scales = "free")
gc_long %>%
group_by(Time, Replicate) %>%
summarise(mean=mean(log10(count +1)),se=se(log10(count +1))) %>%
ggplot(aes(x=Time, y=mean, color = Replicate)) +
facet_wrap(~Replicate) +
geom_line(aes(group=1), stat= "identity", size = 2) +
scale_x_discrete(limits= c("t0", "t2", "t24")) +
scale_y_continuous(limits = c(0.4,0.8), breaks = seq(0.4,0.8,0.05)) +
guides(color="none") +
ylab(label = "mean(log10(count + 1))") +
theme_light() +
theme(axis.text = element_text(face="bold", size=12),
axis.title = element_text(face="bold", color = "#C84DF9", size=14),
axis.ticks = element_blank())
p4 <- ggplot(data=iris,mapping=aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(size = 3, alpha = 0.6) +
theme_classic(base_size = 12) +
border()
d1 <- ggplot(data=iris,mapping=aes(Sepal.Length, fill = Species)) +
geom_density(alpha = 0.6) +
theme_classic() +
clean_theme() +
theme(legend.position = "none")
d2 <- ggplot(data=iris,mapping=aes(Sepal.Width, fill = Species)) +
geom_density(alpha = 0.6) +
theme_classic() +
clean_theme() +
theme(legend.position = "none") +
rotate()
ggarrange(d1, NULL, p4, d2,
ncol = 2, nrow = 2, align = "hv",
widths = c(3, 1), heights = c(1, 3),
common.legend = TRUE)
Eigenvalues <- gc_mds$eig
Variance <- Eigenvalues / sum(Eigenvalues)
Variance1 <- 100 * signif(Variance[1], 3)
Variance2 <- 100 * signif(Variance[2], 3)
gc_mds_long <- gc_mds$points %>%
as.data.frame() %>%
rownames_to_column("Sample_ID") %>%
full_join(md, by = "Sample_ID")
gc_mds_long$Sample_Name <- factor(gc_mds_long$Sample_Name, levels = c("t0_A","t0_B","t0_C","t2_A","t2_B","t2_C","t6_A","t6_B","t6_C","t24_A","t24_B","t24_C"))
gc_mds_long$Time <- factor(gc_mds_long$Time, levels = c("t0","t2","t6","t24"))
gc_mds_long$Replicate <- factor(gc_mds_long$Replicate, levels = c("A","B","C"))
ggplot(gc_mds_long, aes(x=V1, y=V2, color = Time)) +
geom_point(size = 3, aes(shape = Replicate)) +
xlab(paste("PCO1: ", Variance1, "%")) +
ylab(paste("PCO2: ", Variance2, "%")) +
geom_vline(xintercept = 0, linetype=2) +
geom_hline(yintercept = 0, linetype=2) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
##
## locale:
## [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
## [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
## [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] shiny_1.8.0 treeio_1.27.0.002 ggtree_3.11.0
## [4] pheatmap_1.0.12 swemaps_1.0 mapdata_2.3.1
## [7] maps_3.4.2 gridExtra_2.3 jpeg_0.1-10
## [10] ggpubr_0.6.0 cowplot_1.1.3 ggthemes_5.0.0
## [13] scales_1.3.0 ggrepel_0.9.5 wesanderson_0.3.7
## [16] forcats_1.0.0 stringr_1.5.1 purrr_1.0.2
## [19] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
## [22] tidyverse_2.0.0 reshape2_1.4.4 ggplot2_3.4.4
## [25] formattable_0.2.1 kableExtra_1.4.0 dplyr_1.1.4
## [28] lubridate_1.9.3 leaflet_2.2.1 yaml_2.3.8
## [31] fontawesome_0.5.2.9000 captioner_2.2.3 bookdown_0.37
## [34] knitr_1.45
##
## loaded via a namespace (and not attached):
## [1] nlme_3.1-155 fs_1.6.3 RColorBrewer_1.1-3
## [4] tools_4.1.3 backports_1.4.1 bslib_0.6.1
## [7] utf8_1.2.4 R6_2.5.1 lazyeval_0.2.2
## [10] mgcv_1.8-39 colorspace_2.1-0 withr_3.0.0
## [13] tidyselect_1.2.0 compiler_4.1.3 cli_3.6.2
## [16] xml2_1.3.6 labeling_0.4.3 sass_0.4.8
## [19] yulab.utils_0.1.4 systemfonts_1.0.5 digest_0.6.34
## [22] rmarkdown_2.25 svglite_2.1.3 pkgconfig_2.0.3
## [25] htmltools_0.5.7 fastmap_1.1.1 highr_0.10
## [28] htmlwidgets_1.6.4 rlang_1.1.3 xaringan_0.28
## [31] rstudioapi_0.15.0 gridGraphics_0.5-1 jquerylib_0.1.4
## [34] farver_2.1.1 generics_0.1.3 jsonlite_1.8.8
## [37] crosstalk_1.2.1 car_3.1-2 magrittr_2.0.3
## [40] ggplotify_0.1.2 patchwork_1.2.0 Matrix_1.6-5
## [43] Rcpp_1.0.12 munsell_0.5.0 fansi_1.0.6
## [46] ape_5.7-1 abind_1.4-5 lifecycle_1.0.4
## [49] stringi_1.8.3 carData_3.0-5 plyr_1.8.9
## [52] promises_1.2.1 parallel_4.1.3 lattice_0.20-45
## [55] splines_4.1.3 hms_1.1.3 pillar_1.9.0
## [58] ggsignif_0.6.4 glue_1.7.0 evaluate_0.23
## [61] ggfun_0.1.4 leaflet.providers_2.0.0 httpuv_1.6.14
## [64] vctrs_0.6.5 tzdb_0.4.0 gtable_0.3.4
## [67] cachem_1.0.8 xfun_0.41 mime_0.12
## [70] xtable_1.8-4 broom_1.0.5 tidytree_0.4.6
## [73] later_1.3.2 rstatix_0.7.2 viridisLite_0.4.2
## [76] aplot_0.2.2 memoise_2.0.1 timechange_0.3.0
## [79] ellipsis_0.3.2