This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Ordering row with high expression at the top rather than the bottom

Hi All,

hope you are well. been trying to arrange order of row in decreasing order, but did not work. Basically, I like to have row with high expression at top and then arrange all row in decreasing order based on their relative expression. I worked previously, but did not work this time. Tried to solve it, but could not fix it. these are fake dataset. have a good day!

library("readxl")
# xls files
cibersort <- read_excel("Cibersort_timecourse_raw.xlsx")

## Convert each column to percentage ================================

## step 1 is to write a function ================

## source https://stackoverflow.com/questions/30457951/convert-columns-i-to-j-to-percentage

myfun <- function(x) {
  if(is.numeric(x)){ 
    ifelse(is.na(x), x, paste0(round(x*100L, 1))) 
  } else x 
}

## Apply mutate each ===========================================

Cibersort_percent<- cibersort %>% mutate_each(funs(myfun)) 

## Perform hierachical clustering =========================

dim(Cibersort_percent)
head(Cibersort_percent)

## Transpose =================================

Cibersort.Tran<- as.data.frame(t(Cibersort_percent[-1]))
names(Cibersort.Tran)= Cibersort_percent$Mixture
rownames(Cibersort.Tran)=names(Cibersort_percent[-1])

## load phenotype data =================

Pheno <- read_excel("Phenotype.xlsx")

## Selection relevant ones =================

phenotype<- Pheno[1:2]

## Scale the numeric variables ==================================

Cibersort.numeric<- Cibersort.Tran %>% mutate_if(is.character,as.numeric)

## Feature scaling ==============================

df.scale<- scale(Cibersort.numeric)

## math row/column of datframe and phenotypic data 

Group_df = data.frame("Group" = phenotype$Group)
rownames(Group_df) = colnames(Cibersort.Tran) # name matching

# plot pheatmap ===========================

library(pheatmap)

pheatmap(df.scale, cluster_cols = T, annotation = select(Group_df, Group))

enter image description here

pheatmap

It is difficult to answer without data, but you could convert Group_df$Group to factor and set the levels in the order you want without clustering. Something like Group_df$Group=factor(Group_df$Group,levels=c("M0.Macrophage","M1.Macrophage", ...)) and then remove the clustering of rows with cluster_rows=F : pheatmap(df.scale, cluster_cols = T, cluster_rows=F, annotation = select(Group_df, Group))

Dear Basti,

Thank you! I will experiment your code chunk. many thanks,

sk,

0 answers

No answers yet.

Log in to answer this question.