Hi,
I am working with a dataframe in R containing the quantitative data and trying to plot a volcano plot using library(EnhancedVolcano
) package. I am currently analyzing by subsetting based on corresponding matching pairs of "Coef" and "P.value" obtained from limma (for instance; Coef.HC_6h_vs_0h and P.value.HC_6h_vs_0h) individually and export the pdf plot. I have many matching paired columns, this process becomes very tedious and cumbersome. I took some help to create a forloop
on each matching pairs of "Coef" and "P.value" to generate separate plots in the form of the pdf file. The exported pdf files exists but it cannot be opened. I have provided the example below. Please advise on how to solve this.
library(tidyverse)
library(EnhancedVolcano)
DF <- structure(list(Coef.HC_6h_vs_0h = c(NA, NA, 0.048151066, 1.130642422,
0.68074318, 0.063779224, -0.358027426, -0.241326901, 0.167703878,
-0.257097748, -2.327977345, 1.300506434, 0.037118733, 0.036362435,
0.018953335, -0.215582086, -0.215130266, -1.642467234, 0.225615042,
0.212244164),
Coef.HC_LTA_vs_6h = c(NA, NA, NA, 0.571320992,
0.240900855, 0.664981117, -0.080211466, 0.007521568, 0.032402696,
0.369452449, 0.934659157, 1.861006387, -0.338416494, 0.127921196,
0.040630241, 0.275547134, 0.952987298, 0.433537994, 0.492574777,
-0.099178916),
Coef.UC_Rem_LPS_vs_6h = c(-0.259935478, -0.105746142,
0.207159012, 1.147068093, 0.211020926, 0.532359869, -0.101653129,
0.20209878, 0.016125514, 1.32593494, 1.496175233, 2.104156173,
-0.667087007, 0.154297917, 0.272351994, 0.791148254, 0.35998438,
1.399002196, 0.196009571, 0.619367312),
Coef.CD_Mil_FLA_vs_6h = c(0.261472621,
-0.25263016, -0.041481308, -0.096265831, 0.004722895, 0.084040658,
-0.048078762, 0.009246748, -0.234490949, 0.273803791, 1.480274922,
0.292870886, -0.143628372, 0.087706716, -0.002301245, 0.15003941,
-0.314001872, 0.24746456, 0.617210833, -0.018220783),
P.value.HC_6h_vs_0h = c(0.545376281,
0.011772197, 0.821228333, 6.67e-05, 0.415510885, 0.796781833,
0.136254462, 0.313415856, 0.589642204, 0.337351766, 0.023366465,
0.017807501, 0.860263888, 0.882854509, 0.956424822, 0.299125336,
0.622690737, 2.11e-06, 0.691578797, 0.474612129),
P.value.HC_LTA_vs_6h = c(0.863203348,
0.465183842, 0.064113571, 0.043226481, 0.768757045, 0.00736856,
0.738392724, 0.97492814, 0.916995649, 0.168106123, 0.249987701,
0.000163064, 0.108745568, 0.598176335, 0.906755014, 0.184550004,
0.026536555, 0.208004113, 0.386500216, 0.73827056),
P.value.UC_Rem_LPS_vs_6h = c(0.580841515,
0.728198309, 0.331085948, 5.23e-05, 0.796764557, 0.031810205,
0.672110689, 0.398510201, 0.958635746, 8.81e-07, 0.107068282,
1.5e-05, 0.001603959, 0.52498963, 0.432437774, 0.000146689, 0.410210239,
5.2e-05, 0.73035002, 0.037133718),
P.value.CD_Mil_FLA_vs_6h = c(0.564348495,
0.406476956, 0.845654459, 0.733110608, 0.995400367, 0.73437091,
0.841329565, 0.969180147, 0.450794691, 0.306914258, 0.148066231,
0.558742168, 0.49581594, 0.717831127, 0.994706662, 0.469835936,
0.456921237, 0.47220793, 0.277934735, 0.951048936)),
class = "data.frame", row.names = c("Gene_1","Gene_2", "Gene_3", "Gene_4", "Gene_5", "Gene_6", "Gene_7", "Gene_8",
"Gene_9", "Gene_10", "Gene_11", "Gene_12", "Gene_13", "Gene_14",
"Gene_15", "Gene_16", "Gene_17", "Gene_18", "Gene_19", "Gene_20"))
DFlong <- DF %>% rownames_to_column() %>% pivot_longer(cols = -rowname, names_pattern = "(Coef|P.value)\\.(.+)",
names_to = c(".value","Case"))
Cases <- unique(DFlong$Case)
for (Case in Cases) {
Data <- filter(DFlong, Case == Case)
pdf(paste0("Plot_for_", Case, ".pdf"), height = 7, width = 7)
EnhancedVolcano(Data,
lab = Data$rowname,
x = 'Coef',
y = 'P.value',
title = Case,
pCutoff = 0.05,
FCcutoff = 1.0,
pointSize = 3.0,
labSize = 6.0)
dev.off()
}
Thank you,
Toufiq
Or use
ggsave()
instead ofpdf(); print(); dev.off()
- it's neater.Ram thank you fo the suggestion. This was helpful as well.
ATpoint, thank you. This fixed my query.