maddy kemen
maddy kemen

Reputation: 77

How to print google map output to pdf in R

At present I am working on sample data,where in I want to show the count of a metric for particular latitude and longitude.

Data is below:

count   latitude    longitude
1   -33.9742299 -59.2025543
1   -32.1833305 -64.4166718
1   40.069099   45.038189
0   43.1708104  -76.2904452
4   51.3105976  4.2649749
3   50.5296991  5.2614551
2   -22.9748764 -44.3036414
6   43.7755615  23.7246154
4   53.1732661  -112.0334845
4   46.315255   -63.325855
0   50.9302435  -113.986716
1   46.402735   -72.274846
0   49.224312   -122.9865026
4   43.8384117  -79.0867579
1   45.0679663  -66.4535262
2   23.132191   113.266531
0   19.912026   109.690508
1   30.4783192  120.9239947
0   39.084158   117.200983
4   49.0812519  16.1921789

library(plotGoogleMaps)
coordinates(nuc)<-~longitude+latitude
proj4string(nuc) <- CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ')
pltmap<-plotGoogleMaps(nuc,filename='firstmp.htm', zcol='count'
                  , draggableMarker=FALSE, colPalette=c('#DC143C', '#FFD700', '#00FF00'))

reference from github-

Now I want to print the map output to pdf.

Checked pdf function,but it is not working for this.Not sure of 'wkhtmltopdf' either as it is other tool.

Please help me with any pointers to get the output in pdf.

Thanks, kemen

Upvotes: 1

Views: 1386

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78832

Here's a possible alternate solution using the ggmap package:

nuc <- read.table(text="count   latitude    longitude
1   -33.9742299 -59.2025543
1   -32.1833305 -64.4166718
1   40.069099   45.038189
0   43.1708104  -76.2904452
4   51.3105976  4.2649749
3   50.5296991  5.2614551
2   -22.9748764 -44.3036414
6   43.7755615  23.7246154
4   53.1732661  -112.0334845
4   46.315255   -63.325855
0   50.9302435  -113.986716
1   46.402735   -72.274846
0   49.224312   -122.9865026
4   43.8384117  -79.0867579
1   45.0679663  -66.4535262
2   23.132191   113.266531
0   19.912026   109.690508
1   30.4783192  120.9239947
0   39.084158   117.200983
4   49.0812519  16.1921789", header=TRUE, stringsAsFactors=FALSE)

library(ggmap)
library(ggthemes)

gmap <- get_map(location=c(longitude=mean(nuc$longitude), 
                           latitude=mean(nuc$latitude)), 
                zoom=3)

gg <- ggmap(gmap)
gg <- gg + geom_point(data=nuc, aes(x=longitude, y=latitude, size=count, fill=count), 
                      shape=21, color="black")
gg <- gg + scale_fill_distiller(palette="Reds")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

That will produce the following which you should be able to use in a PDF pretty easily.

enter image description here

(you may need to tweak the zoom level, etc, as I know some points didn't show up at that zoom level)

UPDATE

Since we're opining about aesthetics, I think the plotGoogleMaps plot looks terrible. But if you really want that as a showcase of your efforts, you can use phantomjs (i.e. you have to install phantomjs). Put this after you plot the map (it relies on that html file being saved):

cat("var page = require('webpage').create();
page.viewportSize = { width: 1280, height: 800 };
page.open('firstmp.htm', function() {
  window.setTimeout(function () {
    page.render('firstmp.png');
    page.render('firstmp.pdf', {format: 'pdf', quality: '100'});
    phantom.exit();
  }, 3000);
});", file="render.js")

system("phantomjs --ignore-ssl-errors=true render.js")

It takes ~3s since it has to let the map tiles load. The result is a png and a pdf file. You can use R markdown if you want to place the png in a PDF with other analysis results around it. Here's the png it created (you can make it any size by changing the width/height params:

enter image description here

You can also set a clipping rectangle to get rid of the controls on the left (and that is an extremely google-able answer).

Upvotes: 3

Related Questions