Search code examples
rplotggplot2density-plot

Draw plot for comparing each row?


I would like to draw a plot for following table.

                       T6     T26   D6     D26
ENSMUSG00000026427    420     170   197    249
ENSMUSG00000026436     27     21    54      77
ENSMUSG00000018189    513    246   429    484
ENSMUSG00000026470    100     55    82     73
ENSMUSG00000026696    147     73   182    283
ENSMUSG00000026568   3620   1571  1264   1746
ENSMUSG00000026504     95     60   569    428

I want to compare each row and specified each column by different colour. X.lab= Gene name y.Lab= Counts


Solution

  • I think that the appropriate plotting choice depends on the characteristics of your full dataset, and from what I can tell, on the number of possible unique values of IDs ("ENSMUSG*") and the possible number of variables ("T26", "D26", ...). What is clear however, is that the variables have different scales, so should not be combined on the same plot, and so I have chosen a faceted grid plot below.

    Here is some code that makes an appropriate choice based on the sample of the data that you have chosen to show us:

    library(readr)
    library(dplyr)
    library(tidyr)
    
    df_foo = read.table(textConnection(
      "T6     T26   D6     D26
    ENSMUSG00000026427    420     170   197    249
    ENSMUSG00000026436     27     21    54      77
    ENSMUSG00000018189    513    246   429    484
    ENSMUSG00000026470    100     55    82     73
    ENSMUSG00000026696    147     73   182    283
    ENSMUSG00000026568   3620   1571  1264   1746
    ENSMUSG00000026504     95     60   569    428"
    ))
    
    # plot the data
    df_foo %>% 
      add_rownames(var = "ID") %>% 
      gather(key = Variable, value = Value, -ID) %>% 
      ggplot(aes(x = ID, y = Value, fill = Variable)) + 
      geom_bar(stat = "identity") + 
      theme_bw() + 
      facet_wrap(~ Variable, scales = "free_y") + 
      theme(axis.text.x = element_text(angle = 50, hjust = 1))
    
    # save the plot
    ggsave("results/faceted_bar.png", dpi = 600)
    

    Note that making the color aesthetic above is strictly not required given that we are faceting by Variable anyway. Here is what the above code produces:

    enter image description here

    It can be easily argued that this is not the appropriate chart for your data given more context and knowledge about your data. You should add more detail to the question as others have commented.