2

Relatively new to R so I apologize if this is a naive question. I am attempting to create a PDF document containing a series of maps (with data point overlays) using prettymapr. Below this, I would like to display some simple text that includes information about that plot (e.g. centroid lat/long). My problem is simply formatting. I'm sure most of my code is not the most elegant, but I created a simplified version below. Both problems are with the second-last "Generate text below plot" step

#Add libraries
library(here)
library(rosm)
library(prettymapr)

#Generate fake data
gpsdata <- data.frame("Clusters" = c(1,2), "Latitudes" = c(38.896357, 37.819039), "Longitudes" = c(-77.036562, -122.478556))

#Initiate PDF
pdf(file = here::here('multiplemaps.pdf'), width = 8, height = 11)

#For loop over individual clusters
for (loc in gpsdata$Clusters){
  
  #Create subsets of relevant datapoints and cluster
  subclusters <- subset(gpsdata,Clusters==loc)
  
  #Create bounding box
  boundbox <- makebbox(n = subclusters$Latitudes[1]+0.005, e = subclusters$Longitudes[1]+0.003, 
                       s = subclusters$Latitudes[1]-0.005, w = subclusters$Longitudes[1]-0.003)
  
  #Split into map and text plot
  layout(mat = matrix(c(1,2),nrow =2, ncol=1),
         heights = c(8,3),
         widths = 1)
  
  #Generate map plot using prettymapr
  prettymap({
    
    #Set map zoom
    my_zoom <- rosm:::tile.raster.autozoom(extract_bbox(matrix(boundbox,ncol=2,byrow=TRUE)),epsg=4326)
    
    #Make primary plot
    osm.plot(boundbox, zoom = my_zoom, zoomin = -1)
    
    #Add centroid in blue
    osm.points(subclusters$Longitudes, subclusters$Latitudes, pch = 21, col = 'black', bg = 'blue',cex = 5)
  },
  
  #Generate text below plot
  title = paste0("\n\n\n\n\nLocation: ", subclusters$Location, "\n", 
                 "Cluster Centroid Coordinates: ", round(subclusters$Latitudes,5), ",",round(subclusters$Longitudes,5)))
}
dev.off()

Problem #1 The text always ends up either not visible or bumped onto the next page. The only solution I have found is very hack-y (adding the title which gets bumped into the next plot using layout) and padding it with a bunch of breaks to make sure it doesn't end up partially underneath the map. This concerns me since in actuality I will be adding some complex variable information here that will vary significantly in length and it seems likely that at some point this will push the text underneath the map again. I have tried adding creating a new blank plot and adding text, but it always ended up interpreting these as different components and putting them on separate pages. I tried adding text alone, but I "think" pdf is specifically looking for plots and won't read it.

Problem #2 The centroid latitude/longitude is included specifically so it can be copy/pasted into google maps. For some reason upon conversion to pdf, the hyphen in the longitude is converted to an en dash, which google maps can't read. Its minor, but annoying. Specifying it as an ascii character (e.g \55) doesn't fix it. I can't believe there isn't some kind of easy fix here, but I can't find one.

Thanks in advance!

Ollie123
  • 21
  • 2
  • 1
    Curious that when I try it, Problem #1 does not appear. The text is perfect. I wonder if you need to update your packages. Problem #2, however, I can see. Maybe specify using unicode? BTW, this is awesome. Nice work so far! – David J. Bosak Feb 05 '21 at 04:48
  • 1
    I also had the hyphen replaced and google maps was not able to read the coordinates. My solution was to use the Cairo device to generate the maps using the function `CairoPDF` from the package `Cairo`, instead of the base `pdf` function, this solved the issue of the hyphen, sadly I am not sure why this happens so it's a lucky workaround. Regarding the title, there might be a problem with encoding? As with David, the titles were all rendered correctly on my computer (I am working in windows, I tried different encodings and did not see any major change) Hope this helps a little! – biofelip Feb 05 '21 at 12:32
  • Appreciate you both - to clarify, the solution I posted for #1 "works" as posted but adding line breaks seems really inelegant and may cause some issues for me down the line. I am hoping there is a way to generate text that is centered in the white space below the plot versus the way I am doing it. Anyone know of one? – Ollie123 Feb 05 '21 at 13:56

0 Answers0