Zack Weinberg
unread,Nov 16, 2016, 1:15:23 PM11/16/16Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ggplot2
I am writing a paper that has lots of small world maps in it, as
background layers for various data visualizations. I'm currently
using essentially this code to draw each map:
#----
suppressPackageStartupMessages({
library(ggplot2)
library(sp)
library(rworldmap)
library(rgdal)
library(ggalt)
library(ggthemes)
})
world <- fortify(getMap())
lseq = seq(-90, 90, by=.25)
boundary <- data.frame(
long = c(rep(-180, length(lseq)), rep(180, length(lseq))),
lat = c(lseq, rev(lseq)))
p <- ggplot() +
geom_polygon(data=boundary, aes(x=long, y=lat), fill="#deebf7") +
geom_map(data=world, map=world, aes(x=long, y=lat, map_id=id),
color="#cccccc", fill="#f7eede", size=0.1) +
scale_x_continuous(limits=c(-180,180), expand=c(0,0)) +
scale_y_continuous(limits=c(-90,90), expand=c(0,0)) +
coord_proj("+proj=wintri") +
theme_map() +
theme(
panel.background=element_blank(),
panel.border=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(0,0,0,0), "lines"))
#----
For a real figure, there will be another layer added to 'p' with data
in it. As far as getting everything drawn, this works fine; the
_problem_ is that the above code, all by itself, produces 85kB of PDF
page-stream data. There are, in total, 50 copies of this map in my
paper (plus two other figures that contain similar map geometry); the
final PDF file comes out to five megabytes, and if my calculations are
correct, 80% of that is copies of the map.
I'd like to ask for suggestions for how to reduce the size of the
final PDF. Abstractly, it seems like it ought to be possible to
define the map layer _once_ for the entire file and then redraw it
under each (sub)figure -- it's my understanding that PDF-the-format is
capable of this ("XObjects"). This would undoubtedly require
cleverness at the LaTeX level as well as during the generation of each
figure. Since the lion's share of the problem is due to repeated
copies of the map within a single faceted figure (one has 20 copies,
another has fifteen) a technique for reducing _those_ to a single copy
of the map each would also help a great deal.
thanks,
zw