title: Dynamic data point label Positioning in ggmap
body: <p>I'm working with the ggmap package in R and I am relatively new to geospatial data visualizations. I have a data frame of eleven latitude and longitude pairs that I would like to plot on a map, each with a label. Here is the dummy data:</p>
  
  <pre><code>lat&lt;- c(47.597157,47.656322,47.685928,47.752365,47.689297,47.628128,47.627071,47.586349,47.512684,47.571232,47.562283)
  
  lon&lt;-c(-122.312187,-122.318039,-122.31472,-122.345345,-122.377045,-122.370117,-122.368462,-122.331734,-122.294395,-122.33606,-122.379745)
  
  labels&lt;-c("Site 1A","Site 1B","Site 1C","Site 2A","Site 3A","Site 1D","Site 2C","Site 1E","Site 2B","Site 1G","Site 2G")
  
  df&lt;-data.frame(lat,lon,labels)
  </code></pre>
  
  <p>Now I use <code>annotate</code> to create the data point labels and plot these on a map;</p>
  
  <pre><code>map.data &lt;- get_map(location = c(lon=-122.3485,lat=47.6200), 
                      maptype = 'roadmap', zoom = 11)
  
  pointLabels&lt;-annotate("text",x=uniqueReach$lon,y=c(uniqueReach$lat),size=5,font=3,fontface="bold",family="Helvetica",label=as.vector(uniqueReach$label))
  
  dataPlot &lt;- ggmap(map.data) +
   geom_point(data = uniqueReach,aes(x = df$lon, y = df$lat), alpha = 1,fill="red",pch=21,size = 6) + labs(x = 'Longitude', y = 'Latitude')+pointLabels
  </code></pre>
  
  <p>This produces a plot of the data points <img src="https://i.stack.imgur.com/0s62F.png" alt="plot of data points with labels"></p>
  
  <p>As you can see, there are two data points that overlap around (-122.44,47.63), and their labels also overlap. Now I can manually add a shift to each label point to keep the labels from overlapping (see <a href="https://stackoverflow.com/questions/21985254/overlapping-labels-ggmap">this post</a>), but this is not a great technique when I need to produce many of these plots for different sets of latitude and longitude pairs. </p>
  
  <p>Is there a way I can automatically keep data labels from overlapping? I realize whether the labels overlap is dependent on the actual figure size, so I'm open to fixing the figure size at certain dimensions if need be. Thank you in advance for any insights!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The following is modified code using the answer given by Sandy Mupratt</p>
  
  <pre><code># Defining function to draw text boxes
  draw.rects.modified &lt;- function(d,...){
    if(is.null(d$box.color))d$box.color &lt;- NA
    if(is.null(d$fill))d$fill &lt;- "grey95"
    for(i in 1:nrow(d)){
      with(d[i,],{
        grid.rect(gp = gpar(col = box.color, fill = fill,alpha=0.7),
                  vp = viewport(x, y, w, h, "cm", c(hjust, vjust=0.25), angle=rot))
      })
    }
    d
  }
  
  
  # Defining function to determine text box borders
  enlarge.box.modified &lt;- function(d,...){
    if(!"h"%in%names(d))stop("need to have already calculated height and width.")
    calc.borders(within(d,{
      w &lt;- 0.9*w
      h &lt;- 1.1*h
    }))
  }
  </code></pre>
  
  <p>Generating the plot:</p>
  
  <pre><code>dataplot&lt;-ggmap(map.data) + 
                   geom_point(data = df,aes(x = df$lon, y = df$lat), 
                              alpha = 1, fill = "red", pch = 21, size = 6) + 
                    labs(x = 'Longitude', y = 'Latitude') +
                    geom_dl(data = df, 
                        aes(label = labels), 
                        list(dl.trans(y = y + 0.3), "boxes", cex = .8, fontface = "bold"))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/vya2v.png" alt="ggmap plot with labels within text boxes"></p>
  
  <p>This is a MUCH more readable plot, but with one outstanding issue. You'll note that the label "Site 1E" begins to overlap the data point associated with "Site 1A". Does directlabels have a way with dealing with labels overlapping data points belonging to another label?</p>
  
  <p>A final question I have regarding this is how can I plot several duplicate labels using this method. Suppose the labels for data.frame are all the same:</p>
  
  <pre><code>df$labels&lt;-rep("test",dim(df)[1])
  </code></pre>
  
  <p>When I use the same code, directlabels removes the duplicate label names:
  <img src="https://i.stack.imgur.com/KJO4a.jpg" alt="enter image description here"></p>
  
  <p>But I want each data point to have a label of "test". Any suggestions?</p>