# HG changeset patch
# User Lance Parsons <lparsons@princeton.edu>
# Date 1400683688 14400
#      Wed May 21 10:48:08 2014 -0400
# Node ID ae2dae0f6a891442a0146d590543eb095241f9d0
# Parent  e4dc83c56938a09ea7d410aff24f0fa4e93da515
Account for label width when labels above or below feature

diff --git a/R/Gviz.R b/R/Gviz.R
--- a/R/Gviz.R
+++ b/R/Gviz.R
@@ -2467,10 +2467,36 @@
                            sa <- spaceAfter
                        },
                        "above"={
-                           sa <- sb <- 0
+                         if(!rev){
+                           featureWidths <- end(finalRanges) - start(finalRanges)
+                           additionalLabelSpace <- ceiling((labelWidths - featureWidths) / 2)
+                           additionalLabelSpace[additionalLabelSpace < 0] <- 0
+                           end(finalRanges) <- end(finalRanges) + additionalLabelSpace
+                           start(finalRanges) <- start(finalRanges) - additionalLabelSpace
+                         }else{
+                           featureWidths <- start(finalRanges) - end(finalRanges)
+                           additionalLabelSpace <- ceiling((labelWidths - featureWidths) / 2)
+                           additionalLabelSpace(additionalLabelSpace < 0) <- 0
+                           end(finalRanges) <- end(finalRanges) - additionalLabelSpace
+                           start(finalRanges) <- start(finalRanges) + additionalLabelSpace
+                         }
+                         sa <- sb <- 0
                        },
                        "below"={
-                           sa <- sb <- 0
+                         if(!rev){
+                           featureWidths <- end(finalRanges) - start(finalRanges)
+                           additionalLabelSpace <- ceiling((labelWidths - featureWidths) / 2)
+                           additionalLabelSpace[additionalLabelSpace < 0] <- 0
+                           end(finalRanges) <- end(finalRanges) + additionalLabelSpace
+                           start(finalRanges) <- start(finalRanges) - additionalLabelSpace
+                         }else{
+                           featureWidths <- start(finalRanges) - end(finalRanges)
+                           additionalLabelSpace <- ceiling((labelWidths - featureWidths) / 2)
+                           additionalLabelSpace(additionalLabelSpace < 0) <- 0
+                           end(finalRanges) <- end(finalRanges) - additionalLabelSpace
+                           start(finalRanges) <- start(finalRanges) + additionalLabelSpace
+                         }
+                         sa <- sb <- 0
                        },
                        stop(sprintf("Unknown label justification '%s'", just)))
                 displayPars(GdObjectOrig) <- list(".__groupLabelWidths"=data.frame(before=sb, label=labelWidths, after=sa),
