This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Reorder by genes, but plot x-axis as chromosomes

Dear All,

I am using ggplot2's generate plots for my RNASeq data.

I would like to plot the genes (Gene_name column), one beside the other in the below dataframe.

Here is the sample data with first 20 rows.

Gene_name   Chr log2FoldChange  padj    significant row
gene1   1   1.291089929 0.545016613 no  1
gene2   1   0.873801059 0.588763458 no  2
gene3   1   0.044171969 0.991428121 no  3
gene4   1   2.105103925 0.017808512 yes 4
gene5   1   0.328333094 0.863293666 no  5
gene6   1   1.174288096 0.150119569 no  6
gene7   1   0.247936928 0.840785644 no  7
gene8   1   0.731217106 0.634092244 no  8
gene9   2   -1.622041186    3.14E-13    yes 9
gene10  2   0   0   no  10
gene11  3   -0.839189648    0.000227869 yes 11
gene12  3   0   0   no  12
gene13  4   -1.31624802 2.26E-08    yes 13
gene14  4   -1.2192487  1.82E-05    yes 14
gene15  4   -0.88556427 0.006486725 yes 15
gene16  5   -0.509988484    0.19660206  no  16
gene17  5   -0.292624116    0.670924418 no  17
gene18  5   -1.509072999    1.20E-06    yes 18
gene19  6   0   0   no  19
gene20  7   -0.766677533    0.122287312 no  20
gene21  8   0.269640649 0.804083309 no  21

When I am plotting using the below command it is giving X axis GeneName and y axis logFoldChange. Since I am using GeneName as x axis aesthetic it is giving all GeneNames on x-axis, but i would like to have unique values of chromosome names (Chr column) rather than GeneNames on X-axis. And also I want to highlight genes that has "yes" in significant column.

I am using the below R code to do that

p = ggplot(data = testDE, mapping = aes(x=reorder(Genename, row), y=log2FoldChange)) + 
geom_point(aes(col= significant))+ 
scale_color_manual(values=c("black", "red"))

I would like to plot aes(x=reorder(Genename, row) but I want x axis text as chromosomes rather than Gene names. When I plot this, it is giving dark black line as x-axis labels because i have more number of genes. Could you please help me to get unique values Chr 1 to 22 rather than Gene names on x axis?

Thanks In Advance Fazulur Rehaman (edited: for formatting)

ggplot2 rna-seq r

Do you want it this way?

Rplot

if so, please replace Gene_name with chr (reorder(Chr, row)).

If you want same plot (as in OP), but for each chromosome separate, do a facet wrap: facet_wrap( ~ Chr, ncol = length(unique(testDE$Chr)), scales = "free"). This would plot X and Y axis on free scale. However, if you want only x-axis free, then change scales="free_x"

1 answer

Maybe facet by chromosomes:

testDE <- read.table(text = "Gene_name   Chr log2FoldChange  padj    significant row
gene1   1   1.291089929 0.545016613 no  1
gene2   1   0.873801059 0.588763458 no  2
gene3   1   0.044171969 0.991428121 no  3
gene4   1   2.105103925 0.017808512 yes 4
gene5   1   0.328333094 0.863293666 no  5
gene6   1   1.174288096 0.150119569 no  6
gene7   1   0.247936928 0.840785644 no  7
gene8   1   0.731217106 0.634092244 no  8
gene9   2   -1.622041186    3.14E-13    yes 9
gene10  2   0   0   no  10
gene11  3   -0.839189648    0.000227869 yes 11
gene12  3   0   0   no  12
gene13  4   -1.31624802 2.26E-08    yes 13
gene14  4   -1.2192487  1.82E-05    yes 14
gene15  4   -0.88556427 0.006486725 yes 15
gene16  5   -0.509988484    0.19660206  no  16
gene17  5   -0.292624116    0.670924418 no  17
gene18  5   -1.509072999    1.20E-06    yes 18
gene19  6   0   0   no  19
gene20  7   -0.766677533    0.122287312 no  20
gene21  8   0.269640649 0.804083309 no  21", header = TRUE, stringsAsFactors = FALSE)

library(ggplot2)

# pre-order the rows by chromosome and gene start positions?
# I am guessing that is what "row" column is doing?
testDE$Gene_name <- factor(testDE$Gene_name, levels = testDE$Gene_name)

ggplot(testDE, aes(x = Gene_name, y = log2FoldChange)) + 
  geom_point(aes(col = significant)) +
  scale_color_manual(values = c("black", "red")) +
  facet_grid(.~paste0("Chr", Chr), scales = "free_x") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust=1))

Dear @zx8754,

Thanks a lot for your quick response & solution to the plot. I used the code above & generated plot. It gives me plot exactly I want.

Thanks again. Fazulur Rehaman

Log in to answer this question.