Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

New to R and having troubles getting layout, prettymapr and pdf to play nicely together

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("




Location: ", subclusters$Location, "
", 
                 "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!

question from:https://stackoverflow.com/questions/66056143/new-to-r-and-having-troubles-getting-layout-prettymapr-and-pdf-to-play-nicely-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...