brennanpincardiff / drawProteins

Creating package to draw proteins from Uniprot API
Other
33 stars 17 forks source link

Control the behaviour of domain labels #28

Open alptaciroglu opened 3 years ago

alptaciroglu commented 3 years ago

I was trying to figure out how to vertically flip domain labels for a large protein.

The issue was solved by modifying draw_domains function. Switched to 'geom_text' layer on top of 'geom_rect' layer (latter serves as a box for the text). The original function uses geom_label layer instead.

Also added scale_fill_npg() function from 'ggsci' package which is totally optional. Dropping this here in case it is useful for others.

library(ggsci)
draw_domains <- function(p,
                         data = data,
                         label_domains = TRUE,
                         label_size = 4,
                         show.legend = TRUE,
                         type = "DOMAIN"){
  begin=end=description=NULL

  p <- p + ggplot2::geom_rect(data= data[data$type == type,],
                              mapping=ggplot2::aes(xmin=begin,
                                                   xmax=end,
                                                   ymin=order-0.25,
                                                   ymax=order+0.25,
                                                   fill=description),
                              show.legend = show.legend) + scale_fill_npg()

  if(label_domains == TRUE){
    ## xmin, xmax values can be optimized for better visualization
    p <- p + geom_rect(data = data[data$type == type, ], 
                       ggplot2::aes(xmin = (begin + (end-begin)/2 - 40), xmax = (begin + (end-begin)/2 + 50), 
                                    ymin = (order - 0.05), ymax = (order + 0.05)), fill = "grey80", alpha = 0.75) + 
      ggplot2::geom_text(data = data[data$type == type, ],
                         ggplot2::aes(x = begin + (end-begin)/2, y = order, label = description),
                         size = label_size, angle = 90)
  }

  return(p)
}