Plotting gene Expression per pathway
0
0
Entering edit mode
13 months ago
Assa Yeroslaviz ★ 1.9k

Good morning, I am trying to recreate for my data an image I saw in a journal, as I find it very intuitive. The image is attached below.

For my data I have for each GO category a list of named vectors containing the log2-FC values for each of the genes, but I also have the data as a data.frame.

List of 10
 $ GO1                                 : Named num [1:104] -0.938 -0.874 1.044 1.209 -1.019 ...
  ..- attr(*, "names")= chr [1:104] "SFRP2" "COL4A1" "NRCAM" "PTGIS" ...
 $ GO2                            : Named num [1:69] -1.701 -1.622 1.892 -1.189 0.807 ...
  ..- attr(*, "names")= chr [1:69] "APOE" "GUCY1A1" "HSPB7" "GUCY1B1" ...

I'm just not sure, how to plot the bars for each of the vectors using ggplot. I think, one can do this with a barplot of height 1 by adding another column? But this seems to be just a workaround and not the best solutions.

I would happy to hear of other, more to the point solutions.

thanks

Assa

GO-enrichment

GO-enrichment gene-expression R ggplot RNA-seq • 862 views
ADD COMMENT
0
Entering edit mode

I think you can start with geom_tile, for example this code can help you to create the general shape of your plot and then you can customize and then arrange individual plots (maybe with ggarrange ?) :

data=data.frame(gene=c("A1","A2","A3","A4","A5","A6"),
                logFC=c(-1,4,6,-3,2,5),
                ont=c("BP","BP","BP","BP","BP","BP"))

ggplot( ) +
  geom_tile(data=data,
            aes(x=reorder(gene,logFC),y=ont, fill=logFC),
            colour="white")+
  scale_fill_gradient2(midpoint=0, mid="white",
                       low=rgb(17/255,119/255,51/255),
                       high=rgb(204/255,102/255,119/255),na.value = "lightgrey",name="logFC") +
  theme_classic()+
  theme(axis.text.y = element_blank(),
        axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line=element_blank())

enter image description here

ADD REPLY
0
Entering edit mode

thanks Basti for the suggestions. I sort of have now a good start, but this is still not exactly good enough

this is ggplot command

ggplot(circ.small, aes(reorder(genes,logFC), term)) + 
  geom_tile(mapping = aes(fill= logFC)) +
  scale_fill_gradient2("Significance", space = "Lab", midpoint=0, mid="white",
                       low=rgb(17/255,119/255,51/255),
                       high=rgb(204/255,102/255,119/255), 
                       guide = guide_colourbar(title.position = "top", title.hjust = 0.5), 
                       breaks = c(min(circ.small$logFC), 0, max(circ.small$logFC)), 
                       labels = c(format(min(circ.small$logFC), digits =3), 
                                  0, format(max(circ.small$logFC), digits =3))) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, size = 1,
                                   hjust = 1), axis.line = element_line(colour = "grey80"), 
        axis.text.y=element_blank(), axis.ticks.y=element_blank(), 
        axis.ticks = element_line(colour = "grey80"), 
 #       axis.title = element_text(size = 1, face = "bold"), 
        axis.text = element_text(size = 3), panel.background = element_blank(), 
        panel.border = element_blank(), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        plot.background = element_blank(), strip.background = element_blank(),
        strip.text.x = element_text(size = 5, colour = "red"),
        legend.position = "right") +
  labs(title = "", x = "Genes", y = "") + 
#  coord_fixed(ratio = 3) +
  facet_wrap(~term,scales = "free")

and the image resulting from this is also attached at the bottom.

I would like to know if there is a possibility to for each of the facets a fixed width for each gene. For now, the width of the last facet with only two genes is exactly the same as the one with over 100 genes. Is it possible to set a fixed width for the facets?

thanks again

ggplot

ADD REPLY
1
Entering edit mode

From what I have quickly found on Stack Overflow, one way could be to use egg::set_panel_size to fix the width of the geom_tile (https://stackoverflow.com/questions/20638294/geom-tile-and-facet-grid-facet-wrap-for-same-height-of-tiles)

Actually this is the result if I do it manually for two plots, I think you can adapt with for loop or apply to make the code clearer :

data=data.frame(gene=c("A1","A2","A3","A4","A5","A6"),
                logFC=c(-1,4,6,-3,2,5),
                ont=c("BP","BP","BP","BP","BP","BP"))

p1=ggplot( ) +
  geom_tile(data=data,
            aes(x=reorder(gene,logFC),y=ont, fill=logFC),
            colour="white")+
  scale_fill_gradient2(midpoint=0, mid="white",
                       low=rgb(17/255,119/255,51/255),
                       high=rgb(204/255,102/255,119/255),na.value = "lightgrey",name="logFC") +
  theme_classic()+
  theme(axis.text.y = element_blank(),
        axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line=element_blank())


data2=data.frame(gene=c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"),
                logFC=c(-1,4,6,-3,2,5,12,-6,7,-4),
                ont=c("BP","BP","BP","BP","BP","BP","BP","BP","BP","BP"))

p2=ggplot( ) +
  geom_tile(data=data2,
            aes(x=reorder(gene,logFC),y=ont, fill=logFC),
            colour="white")+
  scale_fill_gradient2(midpoint=0, mid="white",
                       low=rgb(17/255,119/255,51/255),
                       high=rgb(204/255,102/255,119/255),na.value = "lightgrey",name="logFC") +
  theme_classic()+
  theme(axis.text.y = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line=element_blank())

p1 <- egg::set_panel_size( p1, height=unit(ncol(data), "cm"),
                           width=unit(nrow(data), "cm") )
p2 <- egg::set_panel_size( p2, height=unit(ncol(data2), "cm"),
                           width=unit(nrow(data2), "cm") )
cowplot::plot_grid( p1, p2, nrow=1 )

enter image description here

ADD REPLY
0
Entering edit mode

cool, the egg package looks really good. I'll give it a try. thanks

ADD REPLY

Login before adding your answer.

Traffic: 1530 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6