1

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

maddy kemen
  • 67
  • 2
  • 9
  • jeremycg...I tried the same but encountered the below error: error opening the document.the file cannot be opened as it has no pages.It is still going to webpage – maddy kemen Aug 15 '15 at 14:58
  • pdf("pltmap.pdf") pltmap<-plotGoogleMaps(nuc,filename='firstmp.htm', zcol='count' , draggableMarker=FALSE, colPalette=c('#DC143C', '#FFD700', '#00FF00')) dev.off() – maddy kemen Aug 15 '15 at 15:00
  • what is wrong in above code? – maddy kemen Aug 15 '15 at 15:00
  • my bad - `plotGoogleMaps` makes an html file, not a plot. The pdf() function won't help. Unless someone has an in R solution, I'd open the output file with your favorite browser, and print to pdf – jeremycg Aug 15 '15 at 16:16
  • system("wkhtmltopdf --enable-javascript --javascript-delay 2000 firstmp.htm mymap.pdf") file.copy("mymap.pdf", "firstmp.htm") but could not get any output...not able to get how to exactly use this..any pointers will be appreciated.....installed that software and set in system var path too – – maddy kemen Aug 15 '15 at 19:24

1 Answers1

3

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).

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • thanks for that.But I am looking for some solution for my example too because that is an interactive chart and much more appealing than this.I have tried this too and the points seems to be so close for few of lat and long that they are kind of overlapping even after zoom – maddy kemen Aug 15 '15 at 19:19
  • (you're not going to get the interactivity in the PDF) – hrbrmstr Aug 15 '15 at 19:20
  • system("wkhtmltopdf --enable-javascript --javascript-delay 2000 mymap.html mymap.pdf") file.copy("mymap.pdf", "mymap.html") but could not get any output...not able to get how to exactly use this..any pointers will be appreciated.....installed that software and set in system var path too – maddy kemen Aug 15 '15 at 19:22
  • I am fine with printing the same chart from plotgoogle charts output as static one too.My focus is on the same look as it is more clear – maddy kemen Aug 16 '15 at 01:48
  • hrbrmstr .....Is there a way where I can get a color range in the static chart with ggmap as suggested by you.Basically,I want to have different colors based on count range along with the size what is shown above (0-6 on a color range,may be from red(low) to green (high).I tweaked the zoom as required and got it printed to pdf successfully.Thanks.Now I am looking for color range if there is any. – maddy kemen Aug 16 '15 at 02:08
  • adding colour in aes and scale_color_gradient seems to give error as below:Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0 – maddy kemen Aug 16 '15 at 02:39
  • got color in ggmap after little tweaks....but still looking for html to pdf conversion in R – maddy kemen Aug 16 '15 at 03:16
  • answer updated and should work if you can install `phantomjs` – hrbrmstr Aug 16 '15 at 11:14
  • Thanks a lot hrbmstr.That worked with phantomjs and that is what I exactly wanted.Got results with ggmap also and that too was of much help. – maddy kemen Aug 16 '15 at 15:32