Loading and Cleaning Data After we learn how to load a dataset, we need to also create an object which might contain multiple types of data - categorical, numerical, strings, etc. Before we can visualize this data, we have to be able to separate these types of data into separate types, assigning them into the right variables. 1. Load data 2. Create object for the dataset 3. Check for data being loaded and type of data 4. Prepare a data frame or a matrix of data we will analyze Load data:

#First, we will import our data and store it an an object or data frame
df <- read.table('gene-expression_dataset.txt', sep ='\t', header = TRUE, row.names=1)

Check whether dataset get loaded or not; we can do it by checking the dimensions of data (with the dim() function) or with the head() function

#Write the first 10 rows of the dataset:

head(df)
#Check dimension of data (how many rows and columns) 
dim(df)
## [1] 200  41

Indicating we have 200 rows and 41 columns in our data.

#Check type of data str(df)
str(df)
## 'data.frame':    200 obs. of  41 variables:
##  $ case_1    : num  1.098 0.66 0.432 0.624 1.551 ...
##  $ case_2    : num  5.7501 2.695 0.0623 9.8672 0.2589 ...
##  $ case_3    : num  2.74 4.34 4.78 1.18 1.11 ...
##  $ case_4    : num  0.925 0.844 0.304 0.01 0.255 ...
##  $ case_5    : num  0.231 0.8823 0.0312 0.35 0.4966 ...
##  $ case_6    : num  0.514 1.142 0.01 3.03 0.551 ...
##  $ case_7    : num  1.96 5.505 3.372 3.956 0.393 ...
##  $ case_8    : num  1.88 3.13 1.21 1.86 1.8 ...
##  $ case_9    : num  1.0633 0.8273 0.0864 0.3832 0.6816 ...
##  $ case_10   : num  0.3845 1.2559 0.0384 1.6795 0.5199 ...
##  $ case_11   : num  2.383 5.314 3.712 4.845 0.811 ...
##  $ case_12   : num  1.267 2.727 1.305 0.413 1.52 ...
##  $ case_13   : num  0.564 2.214 0.45 0.879 0.896 ...
##  $ case_14   : num  4.381 4.958 0.292 1.01 0.586 ...
##  $ case_15   : num  0.792 1.779 0.438 2.354 1.369 ...
##  $ case_16   : num  1.249 2.258 0.72 2.099 0.998 ...
##  $ case_17   : num  1.24 2.25 5.5 1.72 0.98 ...
##  $ control_1 : num  0.169 1.354 0.108 0.488 0.289 ...
##  $ control_2 : num  0.287 0.979 0.01 0.767 0.449 ...
##  $ control_3 : num  0.154 0.928 0.651 0.63 0.4 ...
##  $ control_4 : num  0.199 0.99 0.122 0.561 0.193 ...
##  $ control_5 : num  0.0974 0.532 0.01 0.01 0.5647 ...
##  $ control_6 : num  0.01 0.6776 0.01 0.3753 0.0933 ...
##  $ control_7 : num  0.479 1.588 0.1 0.954 0.356 ...
##  $ control_8 : num  0.0279 0.6418 0.1714 1.2309 0.659 ...
##  $ control_9 : num  0.207 0.933 0.117 0.01 0.641 ...
##  $ control_10: num  0.592 0.691 0.561 0.768 0.167 ...
##  $ control_11: num  0.0971 0.5196 0.1487 1.6399 0.4261 ...
##  $ control_12: num  0.0522 0.1815 0.0353 2.2553 0.2997 ...
##  $ control_13: num  0.0318 0.5115 0.0208 0.2628 0.69 ...
##  $ control_14: num  0.234 0.615 0.177 0.01 0.638 ...
##  $ control_15: num  1.112 0.48 0.114 0.281 0.15 ...
##  $ control_16: num  0.0265 0.7368 0.0337 0.01 0.283 ...
##  $ control_17: num  0.0286 0.7881 0.1215 5.0598 0.2015 ...
##  $ control_18: num  0.24 0.545 0.115 0.01 0.187 ...
##  $ control_19: num  0.227 0.8761 0.0692 0.01 0.4001 ...
##  $ control_20: num  0.567 2.335 0.321 1.332 0.233 ...
##  $ control_21: num  0.789 2.946 0.289 0.659 0.57 ...
##  $ control_22: num  0.515 1.8517 0.0428 0.6374 0.5628 ...
##  $ control_23: num  0.4241 2.0924 0.0507 0.01 0.6483 ...
##  $ control_24: num  0.406 0.854 0.154 0.17 0.345 ...

We can also extract the samples names (header, which are present in columns) from our data:

#Extract the sample names (column names) 
Samples <- colnames(df) 
print(Samples)
##  [1] "case_1"     "case_2"     "case_3"     "case_4"     "case_5"    
##  [6] "case_6"     "case_7"     "case_8"     "case_9"     "case_10"   
## [11] "case_11"    "case_12"    "case_13"    "case_14"    "case_15"   
## [16] "case_16"    "case_17"    "control_1"  "control_2"  "control_3" 
## [21] "control_4"  "control_5"  "control_6"  "control_7"  "control_8" 
## [26] "control_9"  "control_10" "control_11" "control_12" "control_13"
## [31] "control_14" "control_15" "control_16" "control_17" "control_18"
## [36] "control_19" "control_20" "control_21" "control_22" "control_23"
## [41] "control_24"

Extract feature names (gene ids)

#We can extract row ids from data 
Genes <- rownames(df)
print(Genes)
##   [1] "FGFBP1"         "PCSK9"          "CHI3L1"         "RP11-1035H13.3"
##   [5] "TNIP3"          "SLC6A14"        "FCGR3B"         "RNF182"        
##   [9] "CXCL3"          "CCR10"          "IFNG"           "JPH1"          
##  [13] "TNFRSF9"        "SAA2"           "LRP8"           "SAA1"          
##  [17] "KCNN3"          "ACPP"           "MYEOV"          "NEUROG3"       
##  [21] "CEACAM7"        "ERCC6L"         "MCM10"          "ALDH1A2"       
##  [25] "GSG2"           "ALDH4A1"        "FAM72D"         "SLC9A7"        
##  [29] "VWA2"           "AGT"            "AUNIP"          "HLA-DOB"       
##  [33] "HGF"            "FCRL5"          "FAM72B"         "CENPI"         
##  [37] "ASPHD2"         "PTPRO"          "TMED7-TICAM2"   "LAIR2"         
##  [41] "PDPN"           "GINS4"          "CLSPN"          "F2RL2"         
##  [45] "ARNTL2"         "DUSP4"          "SPAG4"          "LRG1"          
##  [49] "FCGR1A"         "FIBIN"          "C4BPB"          "TIGIT"         
##  [53] "BMP7"           "FAM83D"         "CLIC6"          "HSPA4L"        
##  [57] "SKA1"           "MMP3"           "DTL"            "SPA17"         
##  [61] "KIF4A"          "MMP12"          "PARPBP"         "TRIP13"        
##  [65] "CLCN2"          "GPT2"           "CENPA"          "HK2"           
##  [69] "MYL4"           "PSAT1"          "FGF7"           "PHLDA2"        
##  [73] "GZMB"           "GALNT5"         "RPL39L"         "NCAPH"         
##  [77] "NETO2"          "BUB1"           "PRR11"          "DKK4"          
##  [81] "MELK"           "KIF2C"          "HMMR"           "DLGAP5"        
##  [85] "E2F5"           "GBP5"           "CHRNA5"         "TNFSF13B"      
##  [89] "CEP55"          "PBK"            "TCHP"           "CXCL11"        
##  [93] "LPL"            "KIAA0101"       "FCGR3A"         "GGH"           
##  [97] "CENPW"          "CCL13"          "MAD2L1"         "CDKN3"         
## [101] "SLC6A13"        "HEATR4"         "PRCD"           "FTCD"          
## [105] "FAM129C"        "CLRN1"          "KLKB1"          "ABCA10"        
## [109] "SEC31B"         "ADAMTS10"       "BMP8A"          "L3MBTL1"       
## [113] "RNF207"         "HPX"            "SYT8"           "TSSK6"         
## [117] "OXT"            "CES4A"          "BEGAIN"         "WNT9B"         
## [121] "SPATA25"        "ZBTB16"         "APOH"           "LCN12"         
## [125] "GNRH1"          "LRRC29"         "PROCA1"         "PPP1R32"       
## [129] "SLC25A27"       "LMNTD2"         "AIFM3"          "VPS37D"        
## [133] "HSF4"           "CEACAM19"       "PTH2R"          "NKD2"          
## [137] "MKRN3"          "ANGPTL6"        "REG3G"          "RASSF10"       
## [141] "AGBL2"          "SYCP2"          "CYP3A7"         "NEURL2"        
## [145] "NPIPB11"        "CNGA1"          "PCSK4"          "CDH16"         
## [149] "SH2D6"          "GABBR1"         "CBS"            "BCO2"          
## [153] "CHST13"         "ZNF257"         "KCNK15"         "CNTFR"         
## [157] "POU5F1"         "KLHDC1"         "ABCA3"          "ZNF354B"       
## [161] "AGER"           "ZNF334"         "VSTM2A"         "DRD4"          
## [165] "BTNL9"          "C17orf67"       "ADHFE1"         "CLDND2"        
## [169] "VSIG10L"        "ASPG"           "C8A"            "CRIP3"         
## [173] "TMEM74B"        "KLRC1"          "TGFBR3L"        "IGF2BP1"       
## [177] "KLK13"          "CD207"          "KISS1"          "RHBDL1"        
## [181] "RP11-347C12.3"  "ANKAR"          "CDK3"           "LIN7B"         
## [185] "MT1M"           "ENHO"           "LSMEM1"         "EGFL8"         
## [189] "ZNF418"         "TXK"            "SH2D1B"         "LBX2"          
## [193] "CD40LG"         "DEFB131"        "ZNF415"         "CCDC152"       
## [197] "CATSPERG"       "KCNG1"          "AC017081.1"     "SLC34A3"

To clean the data, we can remove the missing values by using the na.omit() or na.exclude() functions.

#Remove NA values 
df1 <- na.exclude(df) 
#To check the dimension of data 
dim(df1)
## [1] 200  41

This output indicates that we are left with 200 rows and 41 columns in our data and no NAs are present, hence the dimension will remain same. Descriptive Statistics and Visualization Descriptive statistics helps us understand statistical properties of the full dataset so that we can spot unwanted variation and decide whether we have to transform the data scaling before analysis or visualization:

Box Plot Now let’s visualize the statistical properties of each sample in the dataset using a box plot (also called box and whisker plot)

#Draw boxplot for all samples (for FPKM values) 
boxplot(df1) 

#Add the main title, axis titles, and color
boxplot(df1, main="Boxplot for Case and control data", xlab="", ylab="Gene expression", col="red", las=2, cex.axis = 0.9)

Note: To make the box plot visually informative, we can add the title main=””, colors and to make sure the axis labels are fully visible. To do so, we need to position the axis label vertically “las=2”. To change the orientation back to horizontal, use las=1 or omit this parameter altogether. To define the font size of labels, cex.axis is used. This box plot represents the descriptive statistics of various samples in our dataset visually. X-axis indicates the sample ID and the Y-axis represents the values (gene expression level). The dots in the plot represent the outliers (which are either above or below the maximum and minimum gene expression values for the sample). From the above box plot, it is difficult to interpret the differences between key statistical properties of each sample. Most of the data we can see are outliers. This can be resolved by transforming the data to a more “normal” distribution by changing the scale of the Y axis. One of such methods is log-scale transformation. Before transforming values to the log scale, it is also important to add 1 to each value. Otherwise, there will be a non-numeric log value for the original zero value present in data.

Perform log scale transformation of the data to normalize data and remove outliers

log_df <- log(df1+1) 
#Check the dimension of data 
dim(log_df)
## [1] 200  41

Compute descriptive statistics of data:

#Let’s check the summary statistics of data after log transformation
summ_log <- summary(log_df)
#Print summary statistics of data 
summ_log
##      case_1           case_2            case_3            case_4       
##  Min.   :0.1021   Min.   :0.00995   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.7012   1st Qu.:0.48448   1st Qu.:0.31289   1st Qu.:0.52283  
##  Median :1.0566   Median :0.74213   Median :0.76419   Median :0.77402  
##  Mean   :1.1632   Mean   :0.93618   Mean   :1.08045   Mean   :0.95483  
##  3rd Qu.:1.4565   3rd Qu.:1.22682   3rd Qu.:1.68880   3rd Qu.:1.12247  
##  Max.   :3.0866   Max.   :3.27927   Max.   :3.98563   Max.   :3.24872  
##      case_5            case_6            case_7            case_8      
##  Min.   :0.00995   Min.   :0.00995   Min.   :0.00995   Min.   :0.0215  
##  1st Qu.:0.36055   1st Qu.:0.45252   1st Qu.:0.26387   1st Qu.:0.3190  
##  Median :0.62723   Median :0.66700   Median :0.68136   Median :0.7668  
##  Mean   :0.77187   Mean   :0.83973   Mean   :0.98644   Mean   :1.0154  
##  3rd Qu.:0.97017   3rd Qu.:1.18222   3rd Qu.:1.47293   3rd Qu.:1.5335  
##  Max.   :2.98263   Max.   :2.66822   Max.   :3.75759   Max.   :3.7431  
##      case_9           case_10           case_11           case_12       
##  Min.   :0.00995   Min.   :0.00995   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.37879   1st Qu.:0.39738   1st Qu.:0.39875   1st Qu.:0.41088  
##  Median :0.62046   Median :0.66798   Median :0.78239   Median :0.82234  
##  Mean   :0.87305   Mean   :0.78991   Mean   :1.04767   Mean   :1.08024  
##  3rd Qu.:1.23354   3rd Qu.:1.07485   3rd Qu.:1.60510   3rd Qu.:1.48472  
##  Max.   :3.16577   Max.   :2.84819   Max.   :3.47888   Max.   :3.89966  
##     case_13          case_14            case_15           case_16      
##  Min.   :0.0188   Min.   :0.009961   Min.   :0.06568   Min.   :0.0425  
##  1st Qu.:0.3497   1st Qu.:0.494055   1st Qu.:0.40246   1st Qu.:0.4960  
##  Median :0.6555   Median :0.810678   Median :0.68710   Median :0.7681  
##  Mean   :0.9703   Mean   :1.038355   Mean   :0.98115   Mean   :1.0202  
##  3rd Qu.:1.3945   3rd Qu.:1.386104   3rd Qu.:1.34705   3rd Qu.:1.3764  
##  Max.   :3.6326   Max.   :3.484378   Max.   :3.31386   Max.   :3.0589  
##     case_17          control_1         control_2         control_3      
##  Min.   :0.00995   Min.   :0.00995   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.23893   1st Qu.:0.76040   1st Qu.:0.60700   1st Qu.:0.62365  
##  Median :0.64196   Median :1.00295   Median :0.91851   Median :0.93882  
##  Mean   :0.99434   Mean   :1.14146   Mean   :0.98907   Mean   :1.02624  
##  3rd Qu.:1.47370   3rd Qu.:1.48400   3rd Qu.:1.26720   3rd Qu.:1.32297  
##  Max.   :4.07530   Max.   :2.99904   Max.   :3.22713   Max.   :3.13754  
##    control_4         control_5         control_6         control_7      
##  Min.   :0.04869   Min.   :0.00995   Min.   :0.00995   Min.   :0.02733  
##  1st Qu.:0.67781   1st Qu.:0.68902   1st Qu.:0.60025   1st Qu.:0.60708  
##  Median :0.94026   Median :0.96610   Median :0.86881   Median :0.92000  
##  Mean   :1.01200   Mean   :1.04576   Mean   :0.97668   Mean   :0.98905  
##  3rd Qu.:1.32909   3rd Qu.:1.36720   3rd Qu.:1.34743   3rd Qu.:1.31723  
##  Max.   :2.79492   Max.   :2.96105   Max.   :2.72988   Max.   :2.63393  
##    control_8         control_9         control_10        control_11     
##  Min.   :0.01527   Min.   :0.00995   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.66030   1st Qu.:0.69109   1st Qu.:0.60079   1st Qu.:0.63055  
##  Median :1.03166   Median :1.03359   Median :0.93264   Median :0.94185  
##  Mean   :1.04745   Mean   :1.10851   Mean   :1.02744   Mean   :1.00481  
##  3rd Qu.:1.35648   3rd Qu.:1.46385   3rd Qu.:1.37440   3rd Qu.:1.37322  
##  Max.   :2.87036   Max.   :2.84382   Max.   :3.01716   Max.   :2.71023  
##    control_12        control_13        control_14        control_15     
##  Min.   :0.00995   Min.   :0.02056   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.33687   1st Qu.:0.51336   1st Qu.:0.44277   1st Qu.:0.36141  
##  Median :0.63271   Median :0.76820   Median :0.67664   Median :0.63005  
##  Mean   :0.72857   Mean   :0.92460   Mean   :0.79166   Mean   :0.80805  
##  3rd Qu.:1.06069   3rd Qu.:1.22473   3rd Qu.:1.01207   3rd Qu.:1.02713  
##  Max.   :2.36610   Max.   :2.51067   Max.   :2.65814   Max.   :3.03619  
##    control_16        control_17        control_18        control_19     
##  Min.   :0.00995   Min.   :0.00995   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.47752   1st Qu.:0.45143   1st Qu.:0.38516   1st Qu.:0.51261  
##  Median :0.77803   Median :0.72242   Median :0.67966   Median :0.80335  
##  Mean   :0.87858   Mean   :0.85490   Mean   :0.79467   Mean   :0.91087  
##  3rd Qu.:1.15452   3rd Qu.:1.13024   3rd Qu.:1.07376   3rd Qu.:1.14325  
##  Max.   :2.89364   Max.   :2.85765   Max.   :2.61133   Max.   :2.97833  
##    control_20        control_21        control_22        control_23     
##  Min.   :0.00995   Min.   :0.05922   Min.   :0.00995   Min.   :0.00995  
##  1st Qu.:0.50972   1st Qu.:0.70841   1st Qu.:0.58280   1st Qu.:0.63435  
##  Median :0.79130   Median :0.96650   Median :0.80122   Median :0.93775  
##  Mean   :0.93261   Mean   :1.07358   Mean   :0.96453   Mean   :1.06107  
##  3rd Qu.:1.10132   3rd Qu.:1.34238   3rd Qu.:1.21742   3rd Qu.:1.33757  
##  Max.   :3.15393   Max.   :3.04136   Max.   :3.00967   Max.   :3.21967  
##    control_24    
##  Min.   :0.0391  
##  1st Qu.:0.5607  
##  Median :0.8004  
##  Mean   :0.9943  
##  3rd Qu.:1.2219  
##  Max.   :3.4710
#Write summary statistics to a file and export it 
write.table(summ_log, file="stat_sum-log-data.txt", col.names=TRUE, sep="\t")

Draw Box plot for data

# Draw boxplot for all samples 
boxplot(log_df, main="Boxplot for log-transformed Data", xlab="", ylab="Gene expression (log[FPKM])", col="red", las=2, cex.axis = 0.9)

Draw box plot for groups of samples (case only)

Group1 <- log_df[,1:17]

Or select group using below command of grep

Group1 <- log_df[grep('^case', names(log_df))]

Extract the specific samples from the data with their name

#Extract specific samples from data with their name 
case <- as.data.frame(log_df[grep('^case', names(log_df))]) 
cont <- as.data.frame(log_df[grep('^control', names(log_df))])

Compute the mean of rows of data

#Compute the mean for each gene within each group 
case <- rowMeans(case) 
cont <- rowMeans(cont)

Bind groups together in the single data frame name as “group”

#Bind groups together into 2 columns 
group <- cbind(case, cont)

Provide new column names

#Provide the column names to both groups 
colnames(group) <- c("case", "control")

Draw a box plot

#Draw boxplot for groups of samples 
boxplot(group, main="Boxplot for groups", xlab="Groups", ylab="Gene expression(log[FPKM])", col=c('cyan', 'pink'))

Scatter Plot Scatterplot between 2 samples (with FPKM values) in the same condition: geom_point() → for relationships between two variables Use geom_dotplot() → for distribution of one variable

 #Extract samples of same group 
sample1 <- df1[,1] 
sample2 <- df1[,2] 
#Combine them column-wise 
s1_2 <- cbind(sample1,sample2) 
#Draw Scatterplot 
plot(s1_2)

ggplot(s1_2, aes(x = sample1, y = sample2)) +
  geom_point()

Scatterplot between 2 samples (with FPKM values) from different conditions:

# Extract samples of different groups 
sample3 <- df1[,1] 
sample4 <- df1[,19] 
# Combine them column-wise 
s3_4 <- cbind(sample3,sample4)
# Draw Scatterplot 
plot(s3_4)

ggplot(s3_4, aes(x = sample3, y = sample4)) +
  geom_point()

Whole data

# draw scatter plot for whole data (with FPKM values)
G1 <- df1 [1:2] 
G2 <- df1[18:20]
G <- cbind(G1,G2)
plot(G)

Bar Plot Now let’s see how we can visualize gene expression for a single sample. We can do this using a bar plot.

#Take the 2nd sample 
sample1 <- log_df[,2] 
#Make a bar plot 
barplot(sample1, main="Barplot of Sample", xlab="Genes", ylab="Gene expression (log[FPKM])")

Correlation plot between samples using corrplot. logdf will be susubsetted for case in 1:17 columns. colorbrewer library will change the color

### correlation plot between genes
logdf_case <- log_df[, 1:17]
cor<- cor(logdf_case)
corrplot(cor, method = c("circle"), type = c("lower"), sig.level = 0.05)

Col<- (brewer.pal(8,"BrBG")) ### adding 8 layers of colors in BrBG 


corrplot(cor, method = c("circle"), type = c("lower"), sig.level = 0.05, 
         col = Col,  # Apply RColorBrewer palette
         order = "hclust",       # Reorder based on clustering
         addCoef.col = "black",  # Add correlation coefficients
         tl.col = "black",       # Text label color
         tl.srt = 45,            # Text label rotation
         number.cex = 0.5)      # Coefficient font size

#Extract 2nd sample with first 20 genes 
sample_20genes <- log_df[1:20,2] ### first 20 genes
# Add colors to the barplot
barplot(sample_20genes,  main="Barplot of Sample for 20 genes", xlab="Genes", ylab="Gene expression (log[FPKM])", col = rainbow(length(sample_20genes)))

Next, let’s see how we can visualize gene expression for a single gene among multiple samples. We can do this using a bar plot.

#Extract 135th gene from the data 
gene_135 <- log_df[135,   ] 
#Convert this data frame into a matrix 
gene_135_m <- as.matrix(gene_135) 
#Extract gene ids list from the data 
Genes <- rownames(log_df) 
#Extract the gene id for 135th gene from list 
gene_id <- Genes[135] 
#Draw barplot for the selected gene 
barplot(gene_135_m, main= gene_id, xlab="", ylab="Gene expression (log[FPKM])", las = 2, cex.names = 0.7, col = "red")

Creating volcano plot creating a function for t-test to find the p-value

#function()
ttest <- function(df, grp1, grp2) {
  x = df[grp1]
  y = df[grp2]
  x = as.numeric(x)
  y = as.numeric(y)  
  results = t.test(x, y)
  results$p.value
}

#Calculating P values

# P value < 0.01 and fold change of > 1.5 (log2FC of 0.58)
#calculating p-value
rawpvalue = apply(df, 1, ttest, grp1 = c(1:17), grp2 = c(18:41))

####transform our data into log2 base.
log_df2 = log2(df)

##calculate the mean of each gene per control group
control = apply(log_df2[,18:41], 1, mean)

#calculate the mean of each gene per case group
case = apply(log_df2[, 1:17], 1, mean) 

#confirming that we have a vector of numbers
class(control)
## [1] "numeric"
class(case)
## [1] "numeric"
#because our data is already log2 transformed, we can take the difference between the means.
##And this is our log2 Fold Change or log2 Ratio == log2(control / test)##
foldchange <- case - control

# Transform the p-value (-1*log(p-value)) and create a volcano plot using ggplot2
results = cbind(foldchange, rawpvalue)
results = as.data.frame(results)
results$genename <- rownames(results)
results$delabel[1:100] <- "up"
results$delabel[101:200] <- "down"

#Plotting

library(ggplot2)
ggplot(results, aes(x=foldchange, y=-1*log10(rawpvalue))) +
  geom_point(aes(colour = delabel), alpha=0.5, size=4) +
  scale_colour_manual(values = c("up"= "red", "down"="blue"))

#Saving list of Up-regualated and down-regulated genes

##saving list of upregulated genes
write.table(results$genename[1:100], file = "uregulated_genes.txt", sep = "\t", dec = ".", row.names = FALSE, col.names = TRUE, quote=FALSE)

##saving list of downregulated genes
write.table(results$genename[101:200], file = "downregulated_genes.txt", sep = "\t", dec = ".", row.names = FALSE, col.names = TRUE, quote=FALSE)

Heatmap

#Heatmap for all the genes 
M1 <- as.matrix(log_df2)
#install.packages("pheatmap")
library(pheatmap)
## Warning: package 'pheatmap' was built under R version 4.5.3

#ploting heatmap

# Heat map
pdf(file = "Heatmap_all_genes_test.pdf",   # The directory you want to save the file in
    width = 60, # The width of the plot in inches
    height = 60)

pheatmap(M1, 
         cellnote = M1,                # Display cell values as text
         cellnote.fontsize = 5,        # Set the font size
         fontsize = 9,                 # Set the general font size
         main = "Heatmap for all genes")  # Add a main title

#dev.off()

Upreglated genes

#Heatmap for all upregulated genes
M1_up <-as.matrix(log_df2[1:20,])
Matrix_up <- as.matrix(log_df[1:20,])
pdf(file = "Heatmap_upregulated_counts_log_df_2.pdf",   # The directory you want to save the file in
    width = 50, # The width of the plot in inches
    height = 50)
pheatmap(
  M1_up,
  color = colorRampPalette(c("navy", "white", "firebrick3"))(100),  # Custom color palette
  clustering_method = "complete",  # Hierarchical clustering method
  show_rownames = TRUE,  # Hide row names
  show_colnames = TRUE,   # Show column names
  fontsize_row = 20,       # Font size for row labels
  fontsize_col = 20,       # Font size for column labels
)

#dev.off()

Plot of upregulated genes

# Heat map
pdf(file = "Plot_upregulated_genes_1.pdf",   # The directory you want to save the file in
    width = 30, # The width of the plot in inches
    height = 30)
pheatmap(M1_up, 
         cellnote = M1_up,                # Display cell values as text
         cellnote.fontsize = 5,        # Set the font size
         fontsize = 9,                 # Set the general font size
         main = "Heatmap for upregulated genes")  # Add a main title

#dev.off()

Downregulated genes

#Heatmap for all downregulated genes
M1_down <-as.matrix(log_df2[101:200,])

# Heat map
pdf(file = "Plot_downregulated_genes_1.pdf",   # The directory you want to save the file in
    width = 70, # The width of the plot in inches
    height = 70)
pheatmap(M1_down, 
         cellnote = M1_down,                # Display cell values as text
         cellnote.fontsize = 5,        # Set the font size
         fontsize = 9,                 # Set the general font size
         main = "Heatmap for upregulated genes")  # Add a main title

#dev.off()

End of Session