I built a shiny app which presents several plot types. However, the plots are cut from the top, and they are too wide. I tried modifying the width and height in the plotOutput
function, did not work. I also thought of using the par function but didn't work for me, perhaps I used it wrong.
My code:
ui <- fluidPage(theme = shinytheme('united'),
titlePanel(title = h3("Graphs - ordered chronologically", align="center")),
selectInput("Plot",
"Choose which plots to present",
choices = list(Heatmap = "Heatmap", PCA = "PCA", VolcanoPlot = "VolcanoPlot", GSEA = 'GSEA')),
submitButton(text = "Show plots"),
verticalLayout( plotOutput(outputId = "PART.1", width = '70%')),
)
## Processing of the data - the server is the back-end that processes the input values
server <- function(input, output) {
# RNA-seq data
raw_counts <- fread('E-MTAB-7805-raw-counts.tsv', data.table = F)
metadata <- fread('E-MTAB-7805-experiment-design.tsv' ,data.table = F)
A=duplicated(raw_counts$`Gene ID`) # Check for duplicates and remove them
raw_counts = raw_counts[!A,]
A=duplicated(raw_counts$`Gene Name`) # Check for duplicates and remove them
raw_counts = raw_counts[!A,]
raw_counts <- raw_counts[!is.na(raw_counts$`Gene ID`), ]
raw_counts <- raw_counts[!is.na(raw_counts$`Gene Name`), ]
Hugo.Symbol <- raw_counts[,c(1:2)]
rownames(raw_counts) <- raw_counts$`Gene Name` # renaming rownames
raw_counts <- raw_counts[, -c(1:2)]
# metadata
C = duplicated(metadata$Run) # Check for duplicates and remove them
metadata = metadata[!C,]
rownames(metadata) <- metadata$Run
metadata <- metadata[,-1]
ind <- order(colnames(raw_counts), rownames(metadata))
raw_counts <- raw_counts[,ind]
# filter
target1 <- c("0 day", "1 day")
Meta_filter1 <- metadata %>% dplyr::filter(`Factor Value[time]` %in% target1)
Counts_filter1 <- raw_counts[intersect(names(raw_counts), rownames(Meta_filter1))]
rownames(Counts_filter1) <- Hugo.Symbol$`Gene Name`
# clean
Counts_filter1 <- Counts_filter1[rowSums(Counts_filter1[])>0,]
thresh <- Counts_filter1 > 1
keep <- rowSums(thresh) >= 2.5
table(keep)
Counts_filter1 <- Counts_filter1[keep,]
# annotate
Meta_filter1$group <- plyr::mapvalues(Meta_filter1$`Factor Value[time]`, c("0 day", "1 day"),
c("CTRL", "CASE"))
ind <- order(colnames(Counts_filter1), rownames(Meta_filter1))
Counts_filter1 <- Counts_filter1[,ind]
# analysis
dds1 <- DESeqDataSetFromMatrix(countData = Counts_filter1,
colData = Meta_filter1,
design = ~ group)
dds1 = DESeq(dds1)
res1 = results(dds1)
res1$symbol <- rownames(res1)
resOrder <- res1[order(res1$padj),]
# heatmap
dds.symbol = dds1
rownames(dds.symbol) = mapIds(org.Hs.eg.db,
keys=rownames(dds1),
column="SYMBOL",
keytype="SYMBOL",
multiVals="first")
rownames(dds.symbol)[is.na(rownames(dds.symbol))] = rownames(dds1)[is.na(rownames(dds.symbol))]
rownames(dds.symbol) = make.unique(rownames(dds.symbol))
selectUp <- resOrder$symbol[resOrder$log2FoldChange>0][1:20]
selectDown <- resOrder$symbol[resOrder$log2FoldChange<0][1:20]
select = c(selectUp,selectDown)
df <- data.frame(row.names = colnames(dds.symbol),
group = colData(dds.symbol)$group)
normcounts1 = assay(vst(dds.symbol,blind=T, nsub = 2000))
# Functional enrichment
res1 = res1[!is.na(res1$padj),]
mygenes1 <- rownames(res1)
lfc_1 = res1$log2FoldChange # get gene symbol
names(lfc_1) <- rownames(res1)
lfc_1 <- sort(lfc_1, decreasing = TRUE)
hallmarks <- msigdbr(species = "Homo sapiens", category = "H") %>%
dplyr::select(gs_name, gene_symbol)
## Output - 1
output$PART.1 <- renderPlot({
if (input$Plot == 'Heatmap') {
pheatmap(normcounts1[select,], cluster_rows=TRUE,
show_colnames = FALSE,cluster_cols=TRUE,
annotation_col=df, scale = 'row',cutree_cols = 2,cutree_rows = 2)
} else if (input$Plot == 'PCA') {
Var <- apply(normcounts1, 1, var)
selectedVarGenes <- names(Var[order(Var, decreasing = T)][1:1000])
M <- t(normcounts1[selectedVarGenes,])
pcaResults = prcomp(M)
qplot(pcaResults$x[,1],pcaResults$x[,2], col=dds1$group,size=2)
} else if (input$Plot == 'VolcanoPlot') {
EnhancedVolcano(resOrder,
lab = resOrder$symbol,
x = 'log2FoldChange',
y = 'padj',
labSize=4,
FCcutoff=2 )
} else {
em1 <- GSEA(lfc_1, TERM2GENE = hallmarks)
dotplot(em1)
}
})
shinyApp(ui = ui, server = server)